Skip to content

Commit

Permalink
Fixed a few issues.
Browse files Browse the repository at this point in the history
* Fixed a zip file writing issue.
* Fixed some errors in definition files.
* Added a new test case.
* Updated version.
  • Loading branch information
pigpigyyy committed Sep 30, 2024
1 parent a89be91 commit 885d46f
Show file tree
Hide file tree
Showing 19 changed files with 580 additions and 204 deletions.
2 changes: 1 addition & 1 deletion Assets/Script/Lib/UI/Control/Basic/ScrollArea.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ local record Param
paddingY: number
visible: boolean
scrollBar: boolean
scrollBarColor3: boolean
scrollBarColor3: number
clipping: boolean
end
local record ScrollArea
Expand Down
2 changes: 1 addition & 1 deletion Assets/Script/Lib/UI/Control/Basic/ScrollArea.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface Param {
paddingY?: number; // default 200
visible?: boolean; // default true
scrollBar?: boolean; // default true
scrollBarColor3?: boolean; // default App.themeColor
scrollBarColor3?: number; // default App.themeColor.toARGB()
clipping?: boolean; // default true
}

Expand Down
29 changes: 18 additions & 11 deletions Assets/Script/Lib/Utils.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ local record Struct
"Modified"
"Updated"
end
__notify: function(event: RecordEvent, key: string, value: any)
metamethod __index: function(self: Struct, key: string): any
metamethod __newindex: function(self: Struct, key: string, item: any)
end
local record StructArray
enum ArrayEvent
"Added"
"Removed"
Expand All @@ -21,28 +26,30 @@ local record Struct
eachAttr: function(self: Struct, handler: function(key: string, value: any))
contains: function(self: Struct, item: any): boolean
count: function(self: Struct): integer
sort: function(comparer: function(any,any):boolean)
__notify: function(event: RecordEvent, key: string, value: any)
__notify: function(event: ArrayEvent, index: integer, value: any)
metamethod __index: function(self: Struct, key: string): any
metamethod __newindex: function(self: Struct, key: string, item: any)
sort: function(comparer: function(any, any):boolean)
__notify: function(event: ArrayEvent, index: integer, item: any)
end
local record StructClass
metamethod __call: function(self: StructClass, values?: table): Struct
end
local record StructArrayClass
metamethod __call: function(self: StructClass, items?: {any}): StructArray
end
local record StructModule
metamethod __index: function(self: StructModule, name: string): StructModule
metamethod __call: function(self: StructModule, ...: string | {string}): StructClass
metamethod __call: function(self: StructModule, fieldName: string, ...: string): StructClass
metamethod __call: function(self: StructModule, fieldNames: {string}): StructClass
metamethod __call: function(self: StructModule): StructArrayClass
end
local record StructHelper
type Type = Struct
type RecordEvent = Struct.RecordEvent
type ArrayEvent = Struct.ArrayEvent
type ArrayEvent = StructArray.ArrayEvent
metamethod __index: function(self: StructHelper, name: string): StructModule
load: function(self: StructHelper, input: string): Struct
load: function(self: StructHelper, input: table): Struct
load: function(self: StructHelper, name: string, input: table): Struct
loadfile: function(self: StructHelper, filename: string): Struct
load: function(self: StructHelper, input: string): any -- Struct | StructArray
load: function(self: StructHelper, input: table): any -- Struct | StructArray
load: function(self: StructHelper, name: string, input: table): any -- Struct | StructArray
loadfile: function(self: StructHelper, filename: string): any -- Struct | StructArray
clear: function(self: StructHelper)
has: function(self: StructHelper, name: string): boolean
end
Expand Down
51 changes: 30 additions & 21 deletions Assets/Script/Lib/Utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,35 @@ type Vec2 = Vec2.Type;

declare module 'Utils' {

type Struct<T = {}> = {
set(index: number, item: any): void;
get(index: number): any;
insert(item: any): void;
class StructType {
private __struct_type__: "Struct";
}

type StructArray<T> = {
set(index: number, item: T & StructType): void;
get(index: number): T & StructType;
insert(item: T & StructType): void;
/** index is 1 based */
insert(index: number, item: any): void;
remove(item: any): any;
insert(index: number, item: T & StructType): void;
remove(item: T & StructType): (T & StructType) | undefined;
/** index is 1 based */
removeAt(index: number): boolean;
clear(): void;
each(handler: (value: any, index: number) => boolean): boolean;
eachAttr(handler: (key: string, value: any) => void): void;
contains(item: any): boolean;
each(handler: (this: void, item: T & StructType, index: number) => boolean): boolean;
contains(item: T & StructType): boolean;
count(): number;
sort(comparer: (a: any, b: any) => boolean): void;
__notify(event: RecordEvent | ArrayEvent | StructEvent, key?: string | number, value?: any): void;
} & T;
sort(comparer: (this: void, a: T, b: T) => boolean): void;
__notify(this: void, event: ArrayEvent, index: number, item: T & StructType): void;
} & StructType;

export const enum StructEvent {
Updated = "Updated"
}
type Struct<T> = {
eachAttr<K extends keyof T>(handler: (this: void, key: K, value: T[K]) => void): void;
__notify<K extends keyof T>(this: void, event: StructEvent, key: K, value: T[K]): void;
} & T & StructType;

export const enum RecordEvent {
export const enum StructEvent {
Modified = "Modified",
Updated = "Updated"
}

export const enum ArrayEvent {
Expand All @@ -38,18 +43,22 @@ export const enum ArrayEvent {

interface StructClass<T> {
(this: void, values?: T): Struct<T>;
(this: void, ...items: any[]): Struct<T>;
}

interface StructArrayClass<T> {
(this: void, items?: (T & StructType)[]): StructArray<T>;
}

interface StructModule {
[name: string]: StructModule;
<T = {}>(this: void, ...fieldNames: string[]): StructClass<T>;
<T = {}>(this: void, fieldNames: string[]): StructClass<T>;
<T>(this: void, fieldName: keyof T, ...fieldNames: (keyof T)[]): StructClass<T>;
<T>(this: void, fieldNames: (keyof T)[]): StructClass<T>;
<T>(this: void): StructArrayClass<T>;
}

type StructHelper = {
load<T = {}>(input: string | Record<string, any>, name?: string): Struct<T>;
loadfile<T = {}>(filename: string): Struct<T>;
load<T>(input: string | {}, name?: string): Struct<T> | StructArray<T>;
loadfile<T>(filename: string): Struct<T> | StructArray<T>;
clear(): void;
has(name: string): boolean;
} & {
Expand Down
10 changes: 5 additions & 5 deletions Assets/Script/Lib/Utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ local _anon_func_0 = function(StructDefs, pairs, tostring) -- 264
return _accum_0 -- 264
end -- 264
Struct = setmetatable({ -- 233
load = function(self, ...) -- 233
load = function(_self, ...) -- 233
local count = select("#", ...) -- 234
if count > 1 then -- 235
local name = select(1, ...) -- 236
Expand All @@ -372,14 +372,14 @@ Struct = setmetatable({ -- 233
return data -- 251
end -- 235
end, -- 233
clear = function(self) -- 252
clear = function(_self) -- 252
StructDefs = { } -- 253
end, -- 252
has = function(self, name) -- 254
has = function(_self, name) -- 254
return (StructDefs[name] ~= nil) -- 254
end -- 254
}, { -- 256
__index = function(self, name) -- 256
__index = function(_self, name) -- 256
local def = StructDefs[name] -- 257
if not def then -- 258
StructHelper.name = name -- 259
Expand All @@ -388,7 +388,7 @@ Struct = setmetatable({ -- 233
end -- 258
return def -- 262
end, -- 256
__tostring = function(self) -- 263
__tostring = function(_self) -- 263
return concat(_anon_func_0(StructDefs, pairs, tostring), "\n") -- 264
end -- 263
}) -- 232
Expand Down
12 changes: 6 additions & 6 deletions Assets/Script/Lib/Utils.yue
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ StructDefMeta = {
item = if index
if 0 < index and index < length
index += 1
remove @,index
remove @, index
else
nil
else
Expand Down Expand Up @@ -230,7 +230,7 @@ StructLoad = (data) ->
for item in *data
StructLoad item
export Struct = setmetatable {
load: (...) =>
load: (_self, ...) ->
count = select "#", ...
if count > 1
name = select 1, ...
Expand All @@ -249,18 +249,18 @@ export Struct = setmetatable {
arg
StructLoad data
data
clear: =>
clear: (_self) ->
StructDefs = {}
has: (name) => StructDefs[name]?
has: (_self, name) -> StructDefs[name]?
}, {
__index: (name) =>
__index: (_self, name) ->
def = StructDefs[name]
if not def
StructHelper.name = name
StructHelper.path = ""
def = StructHelper
def
__tostring: =>
__tostring: (_self) ->
concat [tostring v for _, v in pairs StructDefs], "\n"
}

Expand Down
6 changes: 3 additions & 3 deletions Assets/Script/Test/StructTS.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
local ____exports = {} -- 1
local ____Utils = require("Utils") -- 2
local Struct = ____Utils.Struct -- 2
local Unit = Struct.My.Name.Space.Unit("name", "group", "tag", "actions") -- 11
local Unit = Struct.My.Name.Space.Unit("name", "group", "tag", "actions") -- 15
local Action = Struct.Action("name", "id") -- 16
local Array = Struct.Array() -- 17
local unit = Unit({ -- 20
Expand All @@ -15,7 +15,7 @@ local unit = Unit({ -- 20
Action({name = "sleep", id = "a3"}) -- 27
}) -- 27
}) -- 27
unit.__notify = function(____, event, key, value) -- 32
unit.__notify = function(event, key, value) -- 32
repeat -- 32
local ____switch3 = event -- 32
local ____cond3 = ____switch3 == "Modified" -- 32
Expand All @@ -30,7 +30,7 @@ unit.__notify = function(____, event, key, value) -- 32
end -- 39
until true -- 39
end -- 32
unit.actions.__notify = function(____, event, index, item) -- 44
unit.actions.__notify = function(event, index, item) -- 44
repeat -- 44
local ____switch5 = event -- 44
local ____cond5 = ____switch5 == "Added" -- 44
Expand Down
24 changes: 12 additions & 12 deletions Assets/Script/Test/StructTS.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// @preview-file on
import {ArrayEvent, RecordEvent, Struct, StructEvent} from 'Utils';
import {ArrayEvent, StructEvent, Struct, StructArray} from 'Utils';

// create struct definitions
interface ActionStruct {
name: string;
id: string;
};
interface UnitStruct {
name: string;
group: number;
tag: string;
actions: Struct;
actions: StructArray<ActionStruct>;
};
const Unit = Struct.My.Name.Space.Unit<UnitStruct>("name", "group", "tag", "actions");
interface ActionStruct {
name: string;
id: string;
};
const Action = Struct.Action<ActionStruct>("name", "id");
const Array = Struct.Array();
const Array = Struct.Array<ActionStruct>();

// create instance
const unit = Unit({
Expand All @@ -29,9 +29,9 @@ const unit = Unit({
});

// get notified when record field changes
unit.__notify = (event: RecordEvent | StructEvent, key: string, value) => {
unit.__notify = (event, key, value) => {
switch (event) {
case RecordEvent.Modified:
case StructEvent.Modified:
print(`Value of name \"${key}\" changed to ${value}.`);
break;
case StructEvent.Updated:
Expand All @@ -41,7 +41,7 @@ unit.__notify = (event: RecordEvent | StructEvent, key: string, value) => {
};

// get notified when list item changes
unit.actions.__notify = (event: ArrayEvent | StructEvent, index: number, item: any) => {
unit.actions.__notify = (event, index, item) => {
switch (event) {
case ArrayEvent.Added:
print(`Add item ${item} at index ${index}.`);
Expand All @@ -52,7 +52,7 @@ unit.actions.__notify = (event: ArrayEvent | StructEvent, index: number, item: a
case ArrayEvent.Changed:
print(`Change item to ${item} at index ${index}.`);
break;
case StructEvent.Updated:
case ArrayEvent.Updated:
print("Items updated.");
break;
}
Expand All @@ -65,7 +65,7 @@ unit.actions.removeAt(1);
const structStr = tostring(unit);
print(structStr);

const loadedUnit = Struct.load<UnitStruct>(structStr);
const loadedUnit = Struct.load(structStr) as Struct<UnitStruct>;
for (let i = 1; i <= loadedUnit.actions.count(); i++) {
print(i, loadedUnit.actions.get(i));
}
Expand Down
Loading

0 comments on commit 885d46f

Please sign in to comment.