-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProperties.ahk
More file actions
144 lines (132 loc) · 3 KB
/
Copy pathProperties.ahk
File metadata and controls
144 lines (132 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#Requires AutoHotkey >=2.0
#Include <String>
#Include <Object>
#Include <Error>
#Include <JSON>
/**
* @name Properties.ahk
* @description Simplified saving and loading of JSON data.
* @version 1.0-2026.03.19
* @requires Autohotkey >=2.0
* @license GNU GPLv3
*/
/**
* Interface for saving properties to and loading properties from a file.
* The properties file uses JSON format, which is very compatible with AHK objects.
* Double-underscore-prefixed properties are ignored during JSON serialization.
* @class {Properties}
* @property {String} __filename - Path to the file for saving and loading properties.
* @property {Boolean} __loaded - Flag for when data has been loaded from file.
* @property {Boolean} __saved - Flag for when changes to data have been saved to file.
*/
class Properties {
__filename := ""
__loaded := false
__saved := false
/**
* @constructor
* @param {String} filename
*/
__New(filename) {
Assert(filename is String, "filename must be a String")
this.__filename := filename
}
/**
* Proxy for dynamically getting/setting a property.
* @type {Any}
*/
__Item[prop] {
get {
return this.%prop%
}
set {
this.%prop% := value
this.__saved := false
}
}
/**
* Gets a property's value.
* @param {String} prop - property chain, such as `prop.subprop`
* @returns {Any} the property's value
*/
Get(prop) {
return GetObjectProp(this, prop)
}
/**
* Sets a property's value.
* @param {String} prop - property chain, such as `prop.subprop`
* @param {Any} value - the property's value
* @returns {Any} value
*/
Set(prop, value) {
SetObjectProp(this, prop, value)
this.__saved := false
return value
}
/**
* Checks whether a property exists.
* @param {String} prop - property chain, such as `prop.subprop`
* @returns {Boolean}
*/
Has(prop) {
return ObjectPropExists(this, prop)
}
/**
* Deletes a property.
* @param {String} prop - property chain, such as `prop.subprop`
* @returns {Properties}
*/
Delete(prop) {
DeleteObjectProp(this, prop)
this.__saved := false
return this
}
/**
* Saves to file if changes have been made to this object's properties.
* @returns {Properties} this
*/
Save() {
if (not this.__saved) {
JSON.WriteFile(this.__filename, this, true)
this.__saved := true
}
return this
}
/**
* Loads from file.
* @returns {Properties} this
*/
Load() {
if (not this.__loaded) {
data := JSON.ReadFile(this.__filename)
ObjMerge(this, data)
this.__loaded := true
this.__saved := true
}
return this
}
/**
* Unloads the contents of this object by deleting all of its properties.
* @returns {Properties} this
*/
Unload() {
for k,v in this.OwnProps() {
if (StrStartsWith(k, "__")) {
continue
}
if (this.HasOwnProp(k)) {
this.DeleteProp(k)
}
}
this.__loaded := false
this.__saved := false
return this
}
/**
* Reloads properties from file.
* @returns {Properties} this
*/
Reload() {
return this.Unload().Load()
}
}