forked from ncannasse/castle
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Model.hx
186 lines (165 loc) · 4.79 KB
/
Model.hx
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
* Copyright (c) 2015, Nicolas Cannasse
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
* IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
import cdb.Data;
typedef Prefs = {
windowPos : { x : Int, y : Int, w : Int, h : Int, max : Bool },
curFile : String,
curSheet : Int,
recent : Array<String>,
}
typedef HistoryElement = { d : String, o : String };
class Model {
public var base : cdb.Database;
var prefs : Prefs;
var imageBank : Dynamic<String>;
var openedList : Map<String,Bool>;
var existsCache : Map<String,{ t : Float, r : Bool }>;
var curSavedData : HistoryElement;
var history : Array<HistoryElement>;
var redo : Array<HistoryElement>;
function new() {
openedList = new Map();
prefs = {
windowPos : { x : 50, y : 50, w : 800, h : 600, max : false },
curFile : null,
curSheet : 0,
recent : [],
};
existsCache = new Map();
loadPrefs();
}
function quickExists(path) {
var c = existsCache.get(path);
if( c == null ) {
c = { t : -1e9, r : false };
existsCache.set(path, c);
}
var t = haxe.Timer.stamp();
if( c.t < t - 10 ) { // cache result for 10s
c.r = sys.FileSystem.exists(path);
c.t = t;
}
return c.r;
}
public function getImageData( key : String ) : String {
return Reflect.field(imageBank, key);
}
public function getAbsPath( file : String ) {
return file.charAt(0) == "/" || file.charAt(1) == ":" ? file : new haxe.io.Path(prefs.curFile).dir.split("\\").join("/") + "/" + file;
}
public inline function getSheet( name : String ) {
return base.getSheet(name);
}
public function save( history = true ) {
var sdata = quickSave();
if( history && (curSavedData == null || sdata.d != curSavedData.d || sdata.o != curSavedData.o) ) {
this.history.push(curSavedData);
this.redo = [];
if( this.history.length > 100 || sdata.d.length * (this.history.length + this.redo.length) * 2 > 300<<20 ) this.history.shift();
curSavedData = sdata;
}
if( prefs.curFile == null )
return;
try {
sys.io.File.saveContent(prefs.curFile, sdata.d);
} catch( e : Dynamic ) {
// retry once after EBUSY
haxe.Timer.delay(function() {
sys.io.File.saveContent(prefs.curFile, sdata.d);
},500);
}
}
function saveImages() {
if( prefs.curFile == null )
return;
var img = prefs.curFile.split(".");
img.pop();
var path = img.join(".") + ".img";
if( imageBank == null )
sys.FileSystem.deleteFile(path);
else
sys.io.File.saveContent(path, untyped haxe.Json.stringify(imageBank, null, "\t"));
}
function quickSave() : HistoryElement {
return {
d : base.save(),
o : haxe.Serializer.run(openedList),
};
}
function quickLoad(sdata:HistoryElement) {
base.load(sdata.d);
openedList = haxe.Unserializer.run(sdata.o);
}
public function compressionEnabled() {
return base.compress;
}
function error( msg ) {
js.Browser.alert(msg);
}
function load(noError = false) {
history = [];
redo = [];
base = new cdb.Database();
try {
base.load(sys.io.File.getContent(prefs.curFile));
if( prefs.curSheet > base.sheets.length )
prefs.curSheet = 0;
else while( base.sheets[prefs.curSheet].props.hide )
prefs.curSheet--;
} catch( e : Dynamic ) {
if( !noError ) error(Std.string(e));
prefs.curFile = null;
prefs.curSheet = 0;
}
try {
var img = prefs.curFile.split(".");
img.pop();
imageBank = haxe.Json.parse(sys.io.File.getContent(img.join(".") + ".img"));
} catch( e : Dynamic ) {
imageBank = null;
}
curSavedData = quickSave();
}
function cleanImages() {
if( imageBank == null )
return;
var used = new Map();
for( s in base.sheets )
for( c in s.columns ) {
switch( c.type ) {
case TImage:
for( obj in s.getLines() ) {
var v = Reflect.field(obj, c.name);
if( v != null ) used.set(v, true);
}
default:
}
}
for( f in Reflect.fields(imageBank) )
if( !used.get(f) )
Reflect.deleteField(imageBank, f);
}
function loadPrefs() {
try {
prefs = haxe.Unserializer.run(js.Browser.getLocalStorage().getItem("prefs"));
if( prefs.recent == null ) prefs.recent = [];
} catch( e : Dynamic ) {
}
}
function savePrefs() {
js.Browser.getLocalStorage().setItem("prefs", haxe.Serializer.run(prefs));
}
}