-
Notifications
You must be signed in to change notification settings - Fork 11
/
loader.js
232 lines (220 loc) · 4.82 KB
/
loader.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*\
* loader.js
*
* loader is a requirejs plugin that loads content packages
\*/
define(['LF/packages','LF/global'],function(packages,global){
return {
load: function (name, require, load, config)
{
var path='';
var content={};
var manifest={};
if( config.isBuild)
{
load();
return ;
}
var first,count=0;
for( var i in packages)
{
if( count===0)
first=i;
count++;
}
if( count===1)
{
load_package(packages[first]);
}
function load_package(pack)
{
path=normalize_path(pack.path);
content.location = normalize_path(config.baseUrl)+path;
require( [path+'manifest'], function(mani)
{
manifest=mani;
var manifest_schema=
{
"data":"string",
"properties":"string",
"resourcemap":"string!optional"
}
if( !validate(manifest_schema,manifest))
{
console.log('loader: error: manifest.js of '+path+' is not correct.');
}
require( [path+normalize_file(manifest.data)], load_data);
load_something('properties');
load_something('resourcemap');
});
}
function normalize_path(ppp)
{ //normalize a file path section
if( !ppp)
return '';
ppp=ppp.replace(/\\/g,'/');
if( ppp.charAt(ppp.length-1)!=='/')
ppp+='/';
if( ppp.charAt(0)==='/')
ppp=ppp.slice(1);
return ppp;
}
function normalize_file(ppp)
{
if( !ppp)
return '';
if( ppp.lastIndexOf('.js')===ppp.length-3)
ppp = ppp.slice(0,ppp.length-3);
return ppp;
}
function load_data(datalist)
{
function allow_load(OO)
{
if( typeof global.lazyload==='function')
{
if( !global.lazyload(OO))
return true;
}
else
return true;
}
var datafile_depend=[];
for( var i=0; i<datalist.object.length; i++)
if( allow_load(datalist.object[i]))
datafile_depend.push(path+normalize_file(datalist.object[i].file));
for( var i=0; i<datalist.background.length; i++)
datafile_depend.push(path+normalize_file(datalist.background[i].file));
datafile_depend.push(path+normalize_file(datalist.UI.file));
require( datafile_depend, function()
{
var gamedata={};
for ( var i in datalist)
{
if( i==='object')
gamedata[i]=[];
else if( i==='background')
gamedata[i]=[];
else
gamedata[i]=datalist[i];
}
for( var i=0, j=0; i<datalist.object.length; i++)
{
var O = datalist.object[i];
var obj=
{
id: O.id,
type: O.type
};
if( allow_load(O))
{
obj.data = arguments[j];
j++
}
else
{
obj.data = 'lazy';
obj.file = path+normalize_file(O.file);
}
gamedata.object.push(obj);
}
for( var i=0; i<datalist.background.length; i++,j++)
{
var O = datalist.background[i];
gamedata.background.push({
id: O.id,
data: arguments[j]
});
}
gamedata.UI = arguments[j];
content.data=gamedata;
module_lazyload();
load_ready();
});
}
function load_something(thing)
{
require( [path+normalize_file(manifest[thing])], function(it){
content[thing] = it;
load_ready();
});
}
function load_ready()
{
var content_schema=
{
data:'object',
properties:'object',
resourcemap:'object!optional',
location:'string'
}
if( validate(content_schema,content))
load(content); //make the require loader return
}
function module_lazyload()
{ //embed the lazyload module
if( typeof global.lazyload==='function')
{
content.data.object.load=function(ID,ready)
{
var objects=content.data.object;
var load_list=[];
var res_list=[];
for( var i=0; i<ID.length; i++)
{
var O; //search for the object
for( var j=0; j<objects.length; j++)
if( objects[j].id===ID[i])
{
O=objects[j];
break;
}
if( O && O.data==='lazy')
{
load_list.push(O);
res_list .push(O.file);
}
}
requirejs(res_list,function()
{
for( var i=0; i<arguments.length; i++)
load_list[i].data = arguments[i];
ready();
});
}
}
}
/** a simple JSON schema validator*/
function validate(schema,object)
{
var good=false;
if( object)
{
good=true;
for( var I in schema)
{
var sss = schema[I].split('!'),
type = sss[0],
option = sss[1] || '';
if( typeof object[I]===type) {
//good
}
else if (typeof object[I]==='undefined' &&
option && option==='optional') {
//still good
}
else {
good=false;
break;
}
}
}
return good;
}
},
normalize: function (name, normalize)
{
return name;
}
}
});