Creating models and other scripts

This commit is contained in:
Chris Bell 2025-10-18 19:35:03 -05:00
parent 4e5b37af94
commit 956832c9fb
20 changed files with 279 additions and 10 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
.hidden/ .hidden/
.idea/

View File

@ -1,6 +1,9 @@
[gd_scene format=3 uid="uid://fy5iji5t58jk"] [gd_scene load_steps=2 format=3 uid="uid://fy5iji5t58jk"]
[ext_resource type="Script" uid="uid://b6xurr0segcug" path="res://scripts/test.gd" id="1_ffwby"]
[node name="MainUI" type="CanvasLayer"] [node name="MainUI" type="CanvasLayer"]
script = ExtResource("1_ffwby")
[node name="Panel" type="Panel" parent="."] [node name="Panel" type="Panel" parent="."]
anchors_preset = 15 anchors_preset = 15

View File

@ -0,0 +1,37 @@
# datetime.gd
class_name DateTime
extends RefCounted
var _unix_time: float = 0.0
static func now() -> DateTime:
var dt = DateTime.new()
dt._unix_time = Time.get_unix_time_from_system()
return dt
static func from_unix(unix_seconds: float) -> DateTime:
var dt = DateTime.new()
dt._unix_time = unix_seconds
return dt
static func from_string(input: String) -> DateTime:
var s := input.strip_edges()
if s.ends_with("Z"):
s = s.substr(0, s.length() - 1)
var unix := Time.get_unix_time_from_datetime_string(s)
if s.find("-") == -1 or s.find(":") == -1:
push_error("Invalid datetime string format: %s" % input)
return null
var dt = DateTime.new()
dt._unix_time = unix
return dt
func to_unix() -> float:
return _unix_time
func _to_string() -> String:
var dict := Time.get_datetime_dict_from_unix_time(_unix_time)
return "%04d-%02d-%02dT%02d:%02d:%02dZ" % [
dict.year, dict.month, dict.day,
dict.hour, dict.minute, dict.second
]

View File

@ -0,0 +1 @@
uid://b2k68if5pcfyn

View File

@ -1,12 +1,45 @@
extends RefCounted extends RefCounted
class_name Guid
class_name GUID var bytes: PackedByteArray
var guid: String static func new_guid() -> Guid:
var guid = Guid.new()
guid.bytes = _generate_random_bytes(16)
# Set UUID version (4) and variant bits
guid.bytes[6] = (guid.bytes[6] & 0x0F) | 0x40
guid.bytes[8] = (guid.bytes[8] & 0x3F) | 0x80
return guid
#func _init() -> void: static func from_string(input: String) -> Guid:
#guid = generate_guid() var clean = input.replace("-", "")
if clean.length() != 32:
push_error("Invalid GUID string format")
return null
var guid = Guid.new()
guid.bytes = PackedByteArray()
for i in range(0, 32, 2):
guid.bytes.append(clean.substr(i, 2).hex_to_int())
return guid
# func _to_string() -> String:
#func generate_guid() -> String: var hex = ""
#return str(OS.get_system_time_msecs()) + "-" + str(OS.get_random_float()).replace(".", "") for b in bytes:
hex += "%02x" % b
return (
hex.substr(0,8) + "-" +
hex.substr(8,4) + "-" +
hex.substr(12,4) + "-" +
hex.substr(16,4) + "-" +
hex.substr(20,12)
)
func to_base64() -> String:
return Marshalls.raw_to_base64(bytes)
# --- internal ---
static func _generate_random_bytes(length: int) -> PackedByteArray:
var arr = PackedByteArray()
for i in range(length):
arr.append(randi() & 0xFF)
return arr

View File

@ -1,4 +1,44 @@
extends Object extends RefCounted
class_name DatapackModel class_name DatapackModel
var guid: GUID var guid: Guid
var name: String
var version: String
var author: String
var license: String
var description: String
var icon: String
var created_at: DateTime
var session_zero_version: String
var dependencies: Array[DatapackDependency]
func _init() -> void:
guid = Guid.new_guid()
created_at = DateTime.now()
func to_dict() -> Dictionary:
var deps_arr: Array = []
for d in dependencies:
deps_arr.append(d.to_dict())
return {
"guid": guid.to_string(),
"name": name,
"version": version,
"author": author,
"license": license,
"description": description,
"icon": icon,
"created_at": created_at.to_string(),
"session_zero_version": session_zero_version,
"dependencies": deps_arr
}
# TODO: Implement this
func from_dict(dict: Dictionary) -> DatapackModel:
var dp: DatapackModel = DatapackModel.new()
dp.guid = dict["guid"]
return dp

View File

@ -0,0 +1,19 @@
class_name DatapackDependency
extends RefCounted
var id: Guid
var name: String
var version: String
func to_dict() -> Dictionary:
return {
"id": id.to_string(),
"name": name,
"version": version
}
func from_dict(dict: Dictionary) -> DatapackDependency:
# TODO
return null

View File

@ -0,0 +1 @@
uid://hrv66ufdp4no

View File

@ -0,0 +1,21 @@
class_name DatasetModel
extends SzObject
var dataset_type: String
var entries: Dictionary[String, DatasetEntry]
func to_dict() -> Dictionary:
var dict := super.to_dict()
dict["dataset_type"] = dataset_type
var entries_dict := {}
if entries:
for key in entries.keys():
var de := entries[key]
if de != null:
entries_dict[key] = de.to_dict()
else:
entries_dict[key] = null
dict["entries"] = entries_dict
return dict

View File

@ -0,0 +1 @@
uid://c8nua2p5okuvx

View File

@ -0,0 +1,20 @@
class_name DataFieldValue
extends RefCounted
enum DataFieldType {
TEXT,
MULTILINE_TEXT,
NUMBER,
BOOL,
FORMULA,
LIST
}
var field_type: DataFieldType
var value: Variant
func to_dict() -> Dictionary:
return {
"field_type": field_type,
"value": value # TODO: how do we serialize an uknown type here
}

View File

@ -0,0 +1 @@
uid://cfl5yreaugqde

View File

@ -0,0 +1,32 @@
class_name DatasetEntry
extends RefCounted
var id: String
var name: String
var description: String
var icon: String
var top_level_fields: Dictionary[String, DataFieldValue]
var groups: Array[DatasetGroup]
func to_dict() -> Dictionary:
var groups_array: Array = []
for g in groups:
groups_array.append(g.to_json())
var top_level_fields_dict: Dictionary = {}
if top_level_fields:
for key in top_level_fields.keys():
var dfv: DataFieldValue = top_level_fields[key]
if dfv != null:
top_level_fields_dict[key] = dfv.to_dict()
else:
top_level_fields_dict[key] = null
return {
"id": id,
"name": name,
"description": description,
"icon": icon,
"top_level_fields": top_level_fields_dict,
"groups": groups_array
}

View File

@ -0,0 +1 @@
uid://c266t0ugcrkr

View File

@ -0,0 +1,23 @@
class_name DatasetGroup
extends RefCounted
var id: String
var name: String
var fields: Dictionary[String, DataFieldValue]
func to_json() -> Dictionary:
var fields_dict: Dictionary = {}
if fields:
for key in fields.keys():
var dfv: DataFieldValue = fields[key]
if dfv != null:
fields_dict[key] = dfv.to_dict()
else:
fields_dict[key] = null
return {
"id": id,
"name": name,
"fields": fields_dict
}

View File

@ -0,0 +1 @@
uid://cyr6ocjkgd6vm

View File

@ -0,0 +1,20 @@
class_name SzObject
extends RefCounted
var id: String
var name: String
var sz_type: String
var description: String
var icon: String
var version: String
var schema_version: String
func to_dict() -> Dictionary:
return {
"id": id,
"name": name,
"description": description,
"icon": icon,
"version": version,
"schema_version": schema_version
}

View File

@ -0,0 +1 @@
uid://c8co7n1xvfmou

View File

@ -0,0 +1,12 @@
extends Node
func _enter_tree() -> void:
var dp: DatapackModel = DatapackModel.new()
var dpd: DatapackDependency = DatapackDependency.new()
dpd.id = Guid.new_guid()
dpd.name = "test"
dpd.version = "1.0.0"
dp.dependencies.append(dpd)
var json_string := JSON.stringify(dp.to_dict(), " ")
print(json_string)

View File

@ -0,0 +1 @@
uid://b6xurr0segcug