API Basic Config ko - LIHACHTETAN/ClassicUO-BadNewbie-BasicIDE GitHub Wiki
JSON / Config.bas
ClassicUO โข Basic
Config.bas๋ Scripts/Include์ ์์ต๋๋ค. ๋ฉ์ธ ์คํฌ๋ฆฝํธ ์์ Include/Config.bas๋ฅผ ๋๊ณ Include "Config.bas"๋ฅผ ์ ์ต๋๋ค. ์๊ตฌ JSON๊ณผ ๋ ๋ฆฝ์ ์ธ ๊ธฐ๋ณธ๊ฐ์ ๊ฒฐํฉํฉ๋๋ค.
์ ํํ ๊ตฌ๋ฌธ
Include "Config.bas"
settings = Config.Load(fileName, defaults)
Config.Save(fileName, settings)
enabled = Config.GetFlag(settings, key, fallback=False)
Config.SetFlag(settings, key, value)
JsonParse(text) / JsonStringify(value[, indented=0])
JsonLoad(fileName[, defaultValue]) / JsonSave(fileName, value[, indented=1])
JsonKind(value) / JsonBoolean(value) / JsonNull() / flag.Value()
๋งค๊ฐ๋ณ์
fileNameโ ํ์ ํ์ผ ๊ฒฝ๋ก. ์๋ ๊ฒฝ๋ก๋ Include์์ ํธ์ถํด๋ ๋ฉ์ธ ์คํฌ๋ฆฝํธ ํด๋ ๊ธฐ์ค์ ๋๋ค. ์ ๋ ๊ฒฝ๋ก๋ ํ์ฉํฉ๋๋ค.defaults (Config.Load)โ Config.Load์ ํ์์ธ ๊ธฐ๋ณธ๊ฐ Dictionary์ ๋๋ค. ๊น์ JSON ๋ณต์ฌ๋ณธ์ ์ ์ฅ๋ ์ต์์ ํค๋ฅผ ๋ณํฉํ๋ฉฐ ์๋ณธ์ ๋ณ๊ฒฝํ์ง ์์ต๋๋ค. defaults๋ฅผ ์๋ตํ ์ ์์ต๋๋ค.defaultValue (JsonLoad)โ ํ์ผ์ด๋ ํด๋๊ฐ ์์ ๋๋ง ์ฌ์ฉํ๋ ์ ํ์ ๋์ฒด ๊ฐ. JSON ๋ณํ์ ํตํ ๋ ๋ฆฝ ๋ณต์ฌ๋ณธ์ ๋ฐํํฉ๋๋ค. ๋ด์ฉ, ์ธ์ฝ๋ฉ ๋ฐ ์ ๊ทผ ์ค๋ฅ๋ฅผ ์จ๊ธฐ์ง ์์ต๋๋ค.settings / valueโ ์ซ์, String, ๋ฐฐ์ด, List, Dictionary, JsonBoolean ๋๋ JsonNull. Dictionary ํค๋ String์ด์ด์ผ ํฉ๋๋ค. ์ฐธ์กฐ ๊ณต์ ๋ ํ์ฉํ์ง๋ง ์ํ ์ฐธ์กฐ๋ ์ค๋ฅ์ ๋๋ค.indentedโ Integer ํ๋๊ทธ:0์ ์์ถ ํ์, 0์ด ์๋๋ฉด ๋ค์ฌ์ฐ๊ธฐ. ๊ธฐ๋ณธ๊ฐ:Stringify=0, Save=1.key / fallbackโ Config.Load(fileName, defaults)๋ ์ Dictionary๋ฅผ ๋ฐํํฉ๋๋ค. ์ ์ฅ๋ ์ต์์ ํค๊ฐ defaults์ ๊น์ ๋ณต์ฌ๋ณธ์ ๋ฎ์ด์ฐ๊ณ ์ค์ฒฉ ๊ฐ์ฒด๋ ํต์งธ๋ก ๊ต์ฒดํฉ๋๋ค. Config.Save(fileName, settings)๋ ๋ช ์์ ์ผ๋ก ์ ์ฅํ๋ฉฐ ๋ฐํ๊ฐ์ด ์์ต๋๋ค. Config.GetFlag(settings, key, fallback=False)๋1/True ๋๋0/False๋ฅผ ๋ฐํํ๋ฉฐ ๊ธฐ์กด ๊ฐ์ด ๋ถ๋ฆฌ์ธ์ด ์๋๋ฉด ์ค๋ฅ์ ๋๋ค. Config.SetFlag(settings, key, value)๋ ๋ฉ๋ชจ๋ฆฌ๋ง ๋ณ๊ฒฝํ๊ณ ๋ฐํ๊ฐ์ด ์์ต๋๋ค. Load/Save๋ String ํค Dictionary๊ฐ ํ์ํฉ๋๋ค. ๋ด๋ถ Private RequireObject๋ ๋ฐ๊นฅ ์ข ๋ฅ๋ฅผ ํ์ธํ๊ณ JSON ๋ณํ์ ์ ์ฒด ๋ด์ฉ์ ๊ฒ์ฆํฉ๋๋ค.
๋ฐํ๊ฐ
Config.Load(fileName, defaults)๋ ์ Dictionary๋ฅผ ๋ฐํํฉ๋๋ค. ์ ์ฅ๋ ์ต์์ ํค๊ฐ defaults์ ๊น์ ๋ณต์ฌ๋ณธ์ ๋ฎ์ด์ฐ๊ณ ์ค์ฒฉ ๊ฐ์ฒด๋ ํต์งธ๋ก ๊ต์ฒดํฉ๋๋ค. Config.Save(fileName, settings)๋ ๋ช ์์ ์ผ๋ก ์ ์ฅํ๋ฉฐ ๋ฐํ๊ฐ์ด ์์ต๋๋ค. Config.GetFlag(settings, key, fallback=False)๋1/True ๋๋0/False๋ฅผ ๋ฐํํ๋ฉฐ ๊ธฐ์กด ๊ฐ์ด ๋ถ๋ฆฌ์ธ์ด ์๋๋ฉด ์ค๋ฅ์ ๋๋ค. Config.SetFlag(settings, key, value)๋ ๋ฉ๋ชจ๋ฆฌ๋ง ๋ณ๊ฒฝํ๊ณ ๋ฐํ๊ฐ์ด ์์ต๋๋ค. Load/Save๋ String ํค Dictionary๊ฐ ํ์ํฉ๋๋ค. ๋ด๋ถ Private RequireObject๋ ๋ฐ๊นฅ ์ข ๋ฅ๋ฅผ ํ์ธํ๊ณ JSON ๋ณํ์ ์ ์ฒด ๋ด์ฉ์ ๊ฒ์ฆํฉ๋๋ค.
๋์
- UO. ์๋ ๋ก์ปฌ Basic ํจ์์ด๋ฉฐ ๊ฒ์ ํจํท์ ๋ณด๋ด์ง ์์ต๋๋ค. JsonParse/JsonStringify๋ ๋ฉ๋ชจ๋ฆฌ์์ ์๋ํฉ๋๋ค. Basic True๋ ์ซ์1๋ก ์ ์ฅ๋๋ฏ๋ก JSON true์๋ JsonBoolean(True)๋ฅผ ์ฌ์ฉํฉ๋๋ค. JsonNull()์ null๊ณผ0์ ๊ตฌ๋ถํฉ๋๋ค.
- ์ ํํ ์ซ์๋ง ํ์ฉํฉ๋๋ค. ์ ์ ๊ฐ์ด ยฑ9007199254740991 ๋ฒ์๋ฅผ ๋์ผ๋ฉด ์ค๋ฅ์ด๋ฉฐ ํฐ ID๋ String์ผ๋ก ์ ์ฅํฉ๋๋ค. ๋ค๋ฅธ ์ซ์๋ Double ์ ๋ฐ๋์ ๋๋ค. ์๊ฒฉํ UTF-8์ ์ฌ์ฉํ๋ฉฐ ์ ๋ ฅ BOM์ ํ์ฉํ๊ณ ์ถ๋ ฅ์๋ ์์ต๋๋ค. ์๋ชป๋ Unicode๋ ์ค๋ฅ์ ๋๋ค.
- ์ ํ:1048576 UTF-16 ์ฝ๋ ๋จ์,4MiB,64๋จ๊ณ ์ปจํ ์ด๋,100000 ๊ฐ ๋ ธ๋. ์ด๊ณผํ๋ฉด ์ค๋ฅ์ ๋๋ค. ๋ถ์/์ฝ๊ธฐ๋ ์ ์ปฌ๋ ์ ์ ๋ง๋ค๋ฉฐ ์ ์ฅ์ ๋ฉ๋ชจ๋ฆฌ ๊ฐ์ฒด๋ฅผ ๋ณต์ ํ์ง ์์ต๋๋ค.
- Save๋ ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ฆํ๊ณ ๋ถ๋ชจ ํด๋๋ฅผ ๋ง๋ ๋ค ๋์ ์์ ๊ณ ์ ์์ ํ์ผ์ ์ฐ๊ณ ๋ฒํผ๋ฅผ ๋น์ด ํ ์ด๋/๊ต์ฒดํฉ๋๋ค. ๊ต์ฒด ์ ์คํจ/์ทจ์๋ ๊ธฐ์กด ํ์ผ์ ๋ณด์กดํฉ๋๋ค. OS๊ฐ ํ์ฉํ๋ฉด ์์ ํ์ผ์ ์ ๋ฆฌํฉ๋๋ค. ์๋ฃ๋ ๊ต์ฒด๋ ์ทจ์ํ์ง ์์ต๋๋ค.
- 256๊ฐ ๊ฐ๋ง๋ค,4096๋ฐ์ดํธ/๋ฌธ์ ๋ธ๋ก ์ฌ์ด ๋ฐ ๊ต์ฒด ์ ์ ์ผ์์ ์ง/์ค์ง๋ฅผ ํ์ธํฉ๋๋ค. ์ถ๊ฐ ์ค๋ ๋๋ฅผ ๋ง๋ค์ง ์์ผ๋ฉฐ ๊ฐ๋ณ OS ํธ์ถ์ ๊ฐ์ ์ค๋จํ ์ ์์ต๋๋ค. ๋์ ์ ์ฅ์ ๋ง์ง๋ง ์ฑ๊ณต ๊ต์ฒด๊ฐ ๋จ์ผ๋ฉฐ DB ํธ๋์ญ์ ์ด ์๋๋๋ค.
์์
1. ๋ ๋ฆฝ์ ์ธ ๊ธฐ๋ณธ๊ฐ
# fileName=missing-settings.json์ด๋ฉฐ defaults์ delay=350์ด ์์ต๋๋ค. ํ์ผ์ด ์์ผ๋ฉด Load๊ฐ defaults๋ฅผ ๋ณต์ฌํฉ๋๋ค. ๊ฒฐ๊ณผ๋ฅผ125๋ก ๋ฐ๊ฟ๋ ๊ธฐ์กด350์ ์ ์ง๋์ด ๊ฒฐ๊ณผ๋"125:350"์
๋๋ค. ํ์ผ ๋ถ์ฌ๋ฅผ ์ํํ๊ธฐ ์ ์ ์ด์ ์์ ํ์ผ์ ์ฎ๊ธฐ๊ฑฐ๋ ์ญ์ ํ์ธ์.
Option Explicit On
Include "Config.bas"
Sub Main()
Dim defaults=Dictionary()
defaults["delay"]=350
Dim settings=Config.Load("missing-settings.json", defaults)
settings["delay"]=125
Return CStr(settings["delay"]) & ":" & CStr(defaults["delay"])
End Sub
๋งค๊ฐ๋ณ์ ๋ฐ ์คํ ์ค๋ช :
fileName=missing-settings.json์ด๋ฉฐ defaults์ delay=350์ด ์์ต๋๋ค. ํ์ผ์ด ์์ผ๋ฉด Load๊ฐ defaults๋ฅผ ๋ณต์ฌํฉ๋๋ค. ๊ฒฐ๊ณผ๋ฅผ125๋ก ๋ฐ๊ฟ๋ ๊ธฐ์กด350์ ์ ์ง๋์ด ๊ฒฐ๊ณผ๋"125:350"์ ๋๋ค. ํ์ผ ๋ถ์ฌ๋ฅผ ์ํํ๊ธฐ ์ ์ ์ด์ ์์ ํ์ผ์ ์ฎ๊ธฐ๊ฑฐ๋ ์ญ์ ํ์ธ์.
Include/Config.bas
Option Explicit On
' Copy Include/Config.bas beside your main script, then Include "Config.bas".
' Relative JSON paths are based on the main script's folder, not this module.
Module Config
Private Sub RequireObject(ByVal value)
If JsonKind(value) <> "object" Then
Throw "Config requires a Dictionary with string keys."
End If
End Sub
' Returns a new Dictionary. Saved top-level keys override independent defaults.
' Missing files use defaults; invalid files raise an error and remain unchanged.
Public Function Load(ByVal fileName, ByVal defaults)
RequireObject(defaults)
Dim result = JsonParse(JsonStringify(defaults))
Dim saved = JsonLoad(fileName, Dictionary())
RequireObject(saved)
For Each entry In saved
result.Set(entry.Key(), entry.Value())
Next
Return result
End Function
' No return value. Validates and writes UTF-8 using same-directory replacement.
Public Sub Save(ByVal fileName, ByVal settings)
RequireObject(settings)
JsonSave(fileName, settings)
End Sub
' A JSON Boolean is distinct from a Basic numeric flag. Convert explicitly.
' Returns 1/True or 0/False; non-Boolean saved values raise an error.
Public Function GetFlag(ByVal settings, ByVal key, Optional ByVal fallback=False)
RequireObject(settings)
Dim flag = settings.Get(key, JsonBoolean(fallback))
If JsonKind(flag) <> "boolean" Then
Throw "Config.GetFlag expects a JSON Boolean for key: " & CStr(key)
End If
Return flag.Value()
End Function
' Changes the Dictionary in memory; call Save to persist it.
Public Sub SetFlag(ByVal settings, ByVal key, ByVal value)
RequireObject(settings)
settings.Set(key, JsonBoolean(value))
End Sub
End Module
2. ์ ์ฅ ํ ๋ค์ ์ฝ๊ธฐ
# SetFlag๊ฐ JSON ๋ถ๋ฆฌ์ธ์ ๋ง๋ญ๋๋ค. Save๊ฐ demo-settings.json์ ์์ฑ/๊ต์ฒดํ๊ณ Load๊ฐ ์ฝ์ต๋๋ค. GetFlag๋ Integer1, delay=350์ด๋ฏ๋ก ๊ฒฐ๊ณผ๋"1:350"์
๋๋ค.
Option Explicit On
Include "Config.bas"
Sub Main()
Dim settings=Dictionary()
settings["delay"]=350
Config.SetFlag(settings, "enabled", True)
Config.Save("demo-settings.json", settings)
Dim loaded=Config.Load("demo-settings.json", Dictionary())
Return CStr(Config.GetFlag(loaded, "enabled")) & ":" & CStr(loaded["delay"])
End Sub
๋งค๊ฐ๋ณ์ ๋ฐ ์คํ ์ค๋ช :
SetFlag๊ฐ JSON ๋ถ๋ฆฌ์ธ์ ๋ง๋ญ๋๋ค. Save๊ฐ demo-settings.json์ ์์ฑ/๊ต์ฒดํ๊ณ Load๊ฐ ์ฝ์ต๋๋ค. GetFlag๋ Integer1, delay=350์ด๋ฏ๋ก ๊ฒฐ๊ณผ๋"1:350"์ ๋๋ค.
Include/Config.bas
Option Explicit On
' Copy Include/Config.bas beside your main script, then Include "Config.bas".
' Relative JSON paths are based on the main script's folder, not this module.
Module Config
Private Sub RequireObject(ByVal value)
If JsonKind(value) <> "object" Then
Throw "Config requires a Dictionary with string keys."
End If
End Sub
' Returns a new Dictionary. Saved top-level keys override independent defaults.
' Missing files use defaults; invalid files raise an error and remain unchanged.
Public Function Load(ByVal fileName, ByVal defaults)
RequireObject(defaults)
Dim result = JsonParse(JsonStringify(defaults))
Dim saved = JsonLoad(fileName, Dictionary())
RequireObject(saved)
For Each entry In saved
result.Set(entry.Key(), entry.Value())
Next
Return result
End Function
' No return value. Validates and writes UTF-8 using same-directory replacement.
Public Sub Save(ByVal fileName, ByVal settings)
RequireObject(settings)
JsonSave(fileName, settings)
End Sub
' A JSON Boolean is distinct from a Basic numeric flag. Convert explicitly.
' Returns 1/True or 0/False; non-Boolean saved values raise an error.
Public Function GetFlag(ByVal settings, ByVal key, Optional ByVal fallback=False)
RequireObject(settings)
Dim flag = settings.Get(key, JsonBoolean(fallback))
If JsonKind(flag) <> "boolean" Then
Throw "Config.GetFlag expects a JSON Boolean for key: " & CStr(key)
End If
Return flag.Value()
End Function
' Changes the Dictionary in memory; call Save to persist it.
Public Sub SetFlag(ByVal settings, ByVal key, ByVal value)
RequireObject(settings)
settings.Set(key, JsonBoolean(value))
End Sub
End Module
3. ํ๋๊ทธ ํ์ ํ์ธ
# enabled=1์ ์ซ์์ด๋ฉฐ JSON true๊ฐ ์๋๋๋ค. GetFlag๊ฐ ๊ฑฐ๋ถํ๊ณ Catch๋"invalid flag"๋ฅผ ๋ฐํํฉ๋๋ค. SetFlag(settings,"enabled",True)๋ ์ฌ๋ฐ๋ฅธ ํ์
์ ์ ์ฅํฉ๋๋ค. ํ์ผ์ ๋ณ๊ฒฝํ์ง ์์ต๋๋ค.
Option Explicit On
Include "Config.bas"
Sub Main()
Dim settings=Dictionary()
settings["enabled"]=1
Try
Dim enabled=Config.GetFlag(settings, "enabled")
Catch problem
Return "invalid flag"
End Try
Return "unexpected"
End Sub
๋งค๊ฐ๋ณ์ ๋ฐ ์คํ ์ค๋ช :
enabled=1์ ์ซ์์ด๋ฉฐ JSON true๊ฐ ์๋๋๋ค. GetFlag๊ฐ ๊ฑฐ๋ถํ๊ณ Catch๋"invalid flag"๋ฅผ ๋ฐํํฉ๋๋ค. SetFlag(settings,"enabled",True)๋ ์ฌ๋ฐ๋ฅธ ํ์ ์ ์ ์ฅํฉ๋๋ค. ํ์ผ์ ๋ณ๊ฒฝํ์ง ์์ต๋๋ค.
Include/Config.bas
Option Explicit On
' Copy Include/Config.bas beside your main script, then Include "Config.bas".
' Relative JSON paths are based on the main script's folder, not this module.
Module Config
Private Sub RequireObject(ByVal value)
If JsonKind(value) <> "object" Then
Throw "Config requires a Dictionary with string keys."
End If
End Sub
' Returns a new Dictionary. Saved top-level keys override independent defaults.
' Missing files use defaults; invalid files raise an error and remain unchanged.
Public Function Load(ByVal fileName, ByVal defaults)
RequireObject(defaults)
Dim result = JsonParse(JsonStringify(defaults))
Dim saved = JsonLoad(fileName, Dictionary())
RequireObject(saved)
For Each entry In saved
result.Set(entry.Key(), entry.Value())
Next
Return result
End Function
' No return value. Validates and writes UTF-8 using same-directory replacement.
Public Sub Save(ByVal fileName, ByVal settings)
RequireObject(settings)
JsonSave(fileName, settings)
End Sub
' A JSON Boolean is distinct from a Basic numeric flag. Convert explicitly.
' Returns 1/True or 0/False; non-Boolean saved values raise an error.
Public Function GetFlag(ByVal settings, ByVal key, Optional ByVal fallback=False)
RequireObject(settings)
Dim flag = settings.Get(key, JsonBoolean(fallback))
If JsonKind(flag) <> "boolean" Then
Throw "Config.GetFlag expects a JSON Boolean for key: " & CStr(key)
End If
Return flag.Value()
End Function
' Changes the Dictionary in memory; call Save to persist it.
Public Sub SetFlag(ByVal settings, ByVal key, ByVal value)
RequireObject(settings)
settings.Set(key, JsonBoolean(value))
End Sub
End Module