-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create a property filter store
- Loading branch information
Showing
9 changed files
with
640 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
proxy/src/main/kotlin/net/rsprox/proxy/filters/DefaultPropertyFilterSet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package net.rsprox.proxy.filters | ||
|
||
import net.rsprox.cache.util.atomicWrite | ||
import net.rsprox.shared.StreamDirection | ||
import net.rsprox.shared.filters.PropertyFilter | ||
import net.rsprox.shared.filters.PropertyFilterSet | ||
import net.rsprox.shared.filters.ProtCategory | ||
import java.nio.file.Path | ||
import kotlin.io.path.readText | ||
|
||
public class DefaultPropertyFilterSet( | ||
private val path: Path, | ||
private val creationTime: Long, | ||
private var version: Int, | ||
private var name: String, | ||
private var active: Boolean, | ||
private val filters: MutableMap<String, Boolean>, | ||
) : PropertyFilterSet { | ||
override fun getCreationTime(): Long { | ||
return creationTime | ||
} | ||
|
||
override fun getName(): String { | ||
return name | ||
} | ||
|
||
override fun setName(name: String) { | ||
this.name = name | ||
} | ||
|
||
override fun get(filter: PropertyFilter): Boolean { | ||
return filters[filter.name] ?: filter.enabled | ||
} | ||
|
||
override fun set( | ||
filter: PropertyFilter, | ||
enabled: Boolean, | ||
) { | ||
filters[filter.name] = enabled | ||
save() | ||
} | ||
|
||
override fun set( | ||
category: ProtCategory, | ||
enabled: Boolean, | ||
) { | ||
for (filter in PropertyFilter.entries) { | ||
if (filter.category == category) { | ||
filters[filter.name] = enabled | ||
} | ||
} | ||
save() | ||
} | ||
|
||
override fun set( | ||
streamDirection: StreamDirection, | ||
enabled: Boolean, | ||
) { | ||
for (filter in PropertyFilter.entries) { | ||
if (filter.direction == streamDirection) { | ||
filters[filter.name] = enabled | ||
} | ||
} | ||
save() | ||
} | ||
|
||
override fun setAll(enabled: Boolean) { | ||
for (filter in PropertyFilter.entries) { | ||
filters[filter.name] = enabled | ||
} | ||
save() | ||
} | ||
|
||
override fun setDefaults() { | ||
for (filter in PropertyFilter.entries) { | ||
filters[filter.name] = filter.enabled | ||
} | ||
save() | ||
} | ||
|
||
public fun isActive(): Boolean { | ||
return this.active | ||
} | ||
|
||
public fun setActive(active: Boolean) { | ||
if (this.active == active) return | ||
this.active = active | ||
save() | ||
} | ||
|
||
private fun save() { | ||
path.atomicWrite(buildString(this)) | ||
} | ||
|
||
public companion object { | ||
private const val DEFAULT_VERSION: Int = 0 | ||
|
||
public fun create( | ||
rootPath: Path, | ||
name: String, | ||
): DefaultPropertyFilterSet { | ||
val time = System.currentTimeMillis() | ||
return DefaultPropertyFilterSet( | ||
rootPath.resolve("filters-$time.txt"), | ||
time, | ||
DEFAULT_VERSION, | ||
name, | ||
true, | ||
mutableMapOf(), | ||
).apply { | ||
setDefaults() | ||
} | ||
} | ||
|
||
private fun buildString(propertyFilterSet: DefaultPropertyFilterSet): String { | ||
val builder = StringBuilder() | ||
builder.append("version=").append(propertyFilterSet.version).appendLine() | ||
builder.append("creationtime=").append(propertyFilterSet.creationTime).appendLine() | ||
builder.append("name=").append(propertyFilterSet.name).appendLine() | ||
for ((k, v) in propertyFilterSet.filters) { | ||
builder | ||
.append(k) | ||
.append('=') | ||
.append(v) | ||
.appendLine() | ||
} | ||
return builder.toString() | ||
} | ||
|
||
public fun load(path: Path): DefaultPropertyFilterSet { | ||
val text = path.readText() | ||
var version: Int = -1 | ||
var name: String? = null | ||
var creationTime: Long = 0 | ||
var active: Boolean = false | ||
val properties: MutableMap<String, Boolean> = mutableMapOf() | ||
for (line in text.lineSequence()) { | ||
if (line.startsWith("version=")) { | ||
version = line.substringAfter("version=").toInt() | ||
continue | ||
} | ||
if (line.startsWith("creationtime=")) { | ||
creationTime = line.substringAfter("creationtime=").toLong() | ||
continue | ||
} | ||
if (line.startsWith("name=")) { | ||
name = line.substringAfter("name=") | ||
continue | ||
} | ||
if (line.startsWith("active=")) { | ||
active = line.substringAfter("active=").toBoolean() | ||
continue | ||
} | ||
val signIndex = line.indexOf('=') | ||
if (signIndex == -1) continue | ||
val split = line.split('=') | ||
if (split.size != 2) continue | ||
val (filterName, filterValue) = split | ||
if (filterName.isBlank()) continue | ||
val booleanValue = filterValue.toBooleanStrictOrNull() ?: continue | ||
properties[filterName] = booleanValue | ||
} | ||
check(version != -1) { | ||
"Version not assigned" | ||
} | ||
check(name != null) { | ||
"Name not assigned" | ||
} | ||
return DefaultPropertyFilterSet( | ||
path, | ||
creationTime, | ||
version, | ||
name, | ||
active, | ||
properties, | ||
) | ||
} | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
proxy/src/main/kotlin/net/rsprox/proxy/filters/DefaultPropertyFilterSetStore.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package net.rsprox.proxy.filters | ||
|
||
import net.rsprox.shared.filters.PropertyFilterSet | ||
import net.rsprox.shared.filters.PropertyFilterSetStore | ||
import java.nio.file.Path | ||
|
||
public class DefaultPropertyFilterSetStore( | ||
private val rootPath: Path, | ||
private val filterSets: MutableList<PropertyFilterSet> = mutableListOf(), | ||
) : PropertyFilterSetStore { | ||
override fun create(index: Int): PropertyFilterSet { | ||
if (index == 0) { | ||
throw IllegalArgumentException("Element cannot be created at index 0.") | ||
} | ||
for (filter in filterSets) { | ||
if (filter is DefaultPropertyFilterSet) { | ||
filter.setActive(false) | ||
} | ||
} | ||
val filter = DefaultPropertyFilterSet.create(rootPath, "Unnamed") | ||
filterSets += filter | ||
return filter | ||
} | ||
|
||
override fun delete(index: Int): PropertyFilterSet? { | ||
if (index == 0) throw IllegalArgumentException("Element at index 0 cannot be deleted.") | ||
val element = filterSets.getOrNull(index) ?: return null | ||
filterSets.removeAt(index) | ||
return element | ||
} | ||
|
||
override fun get(index: Int): PropertyFilterSet? { | ||
return filterSets.getOrNull(index) | ||
} | ||
|
||
override fun getActive(): PropertyFilterSet { | ||
val activeFilter = | ||
filterSets | ||
.filterIsInstance<DefaultPropertyFilterSet>() | ||
.find { it.isActive() } | ||
return activeFilter ?: filterSets.first() | ||
} | ||
|
||
override fun setActive(index: Int) { | ||
for (filter in filterSets) { | ||
if (filter is DefaultPropertyFilterSet) { | ||
filter.setActive(false) | ||
} | ||
} | ||
val filterSet = filterSets.getOrNull(index) ?: return | ||
if (filterSet is DefaultPropertyFilterSet) { | ||
filterSet.setActive(true) | ||
} | ||
} | ||
|
||
public companion object { | ||
public fun load(path: Path): PropertyFilterSetStore { | ||
val list: MutableList<PropertyFilterSet> = mutableListOf(UnmodifiablePropertyFilterSet()) | ||
val results = path.toFile().walkTopDown() | ||
for (file in results) { | ||
if (!file.isFile) continue | ||
val set = | ||
try { | ||
DefaultPropertyFilterSet.load(file.toPath()) | ||
} catch (e: Exception) { | ||
continue | ||
} | ||
list += set | ||
} | ||
return DefaultPropertyFilterSetStore(path, list) | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
proxy/src/main/kotlin/net/rsprox/proxy/filters/UnmodifiablePropertyFilterSet.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package net.rsprox.proxy.filters | ||
|
||
import net.rsprox.shared.StreamDirection | ||
import net.rsprox.shared.filters.PropertyFilter | ||
import net.rsprox.shared.filters.PropertyFilterSet | ||
import net.rsprox.shared.filters.ProtCategory | ||
|
||
public class UnmodifiablePropertyFilterSet : PropertyFilterSet { | ||
override fun getCreationTime(): Long { | ||
return Long.MIN_VALUE | ||
} | ||
|
||
override fun getName(): String { | ||
return "Default" | ||
} | ||
|
||
override fun setName(name: String) { | ||
} | ||
|
||
override fun get(filter: PropertyFilter): Boolean { | ||
return filter.enabled | ||
} | ||
|
||
override fun set( | ||
filter: PropertyFilter, | ||
enabled: Boolean, | ||
) { | ||
} | ||
|
||
override fun set( | ||
category: ProtCategory, | ||
enabled: Boolean, | ||
) { | ||
} | ||
|
||
override fun set( | ||
streamDirection: StreamDirection, | ||
enabled: Boolean, | ||
) { | ||
} | ||
|
||
override fun setAll(enabled: Boolean) { | ||
} | ||
|
||
override fun setDefaults() { | ||
} | ||
} |
Oops, something went wrong.