/
Syscalls-for-profiling.cs
3628 lines (2821 loc) · 118 KB
/
Syscalls-for-profiling.cs
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.IO;
using System.Threading;
using System.Net;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Threading;
using System.Windows.Navigation;
using System.Collections.Generic;
using Microsoft.Phone.Tasks;
using System.IO.IsolatedStorage;
using ComponentAce.Compression.Libs.zlib;
using WazeWP7;
public class function_info
{
public string name;
public long start;
public long finish;
public function_info(string name)
{
this.name = name;
this.start = DateTime.UtcNow.Ticks;
}
public function_info setFinishTime()
{
this.finish = DateTime.UtcNow.Ticks;
return this;
}
}
public class Syscalls
{
private static List<UIElement> children_list = new List<UIElement>();
private static int c_on_invokeLater;
private static Random random;
public static List<function_info> fis = new System.Collections.Generic.List<function_info>();
/* These functions have dependencies and are therefore placed here */
public static int __strlen(int src)
{
function_info fi = new function_info("__strlen");
int s = src;
while (CRunTime.memoryReadByte(s) != 0)
s++;
fis.Add(fi.setFinishTime());
return s - src;
}
public static int __strncpy(int dest, int src, int n)
{
function_info fi = new function_info("__strncpy");
while (((dest & 0x3) != 0) && (n > 0))
{
int b = CRunTime.memoryReadByte(src++);
CRunTime.memoryWriteByte(dest, b);
dest++;
n--;
if ((b == 0) || (n == 0))
{
fis.Add(fi.setFinishTime());
return dest;
}
}
while (n > 3)
{
int i = 0;
/* We can probably optimize this by reading a word size if possible */
for (int j = 0; j < 4; j++)
{
i = i << 8;
int b = CRunTime.memoryReadByte(src++) & 0xff;
n--;
i |= b;
if (b == 0)
{
i = i << 8 * (3 - j);
CRunTime.memoryWriteWord(dest, i);
fis.Add(fi.setFinishTime());
return dest;
}
}
CRunTime.memoryWriteWord(dest, i);
dest += 4;
}
while (n > 0)
{
int b = CRunTime.memoryReadByte(src++);
CRunTime.memoryWriteByte(dest, b);
dest++;
n--;
if (b == 0) break;
}
fis.Add(fi.setFinishTime());
return dest;
}
public static int __strncmp(int s1, int s2, int n)
{
function_info fi = new function_info("__strncmp");
int s = s1;
int d = s2;
int a = 0;
int b = 0;
do
{
if (s - s1 >= n)
break;
a = CRunTime.memoryReadByte(s);
b = CRunTime.memoryReadByte(d);
s++;
d++;
} while (a == b && b != 0 && a != 0);
if (a < b)
{
fis.Add(fi.setFinishTime());
return -1;
}
else if (a > b)
{
fis.Add(fi.setFinishTime());
return 1;
}
fis.Add(fi.setFinishTime());
return 0;
}
public static int __strncat(int dest, int src, int n)
{
function_info fi = new function_info("__strncat");
__strncpy(dest + __strlen(dest), src, n);
fis.Add(fi.setFinishTime());
return dest;
}
public static int canvasHandle;
public static int graphicsHandle;
public static int midletHandle;
public static void NOPH_AsyncNet_close(int __async)
{
function_info fi = new function_info("NOPH_AsyncNet_close");
AsyncNet async = (AsyncNet)CRunTime.objectRepository[__async];
async.close();
fis.Add(fi.setFinishTime());
}
public static int NOPH_AsyncNet_isReading(int __async)
{
function_info fi = new function_info("NOPH_AsyncNet_isReading");
AsyncNet async = (AsyncNet)CRunTime.objectRepository[__async];
int ret = (int)async.isReading();
fis.Add(fi.setFinishTime());
return ret;
}
public static int NOPH_AsyncNet_new(int __url, int method, int __update_time, int cb_addr, int context)
{
function_info fi = new function_info("NOPH_AsyncNet_new");
String url = CRunTime.charPtrToString(__url);
String update_time = CRunTime.charPtrToString(__update_time);
AsyncNet ret = (AsyncNet)new AsyncNet(url, method, update_time, cb_addr, context);
int registeredHandle = CRunTime.registerObject(ret);
fis.Add(fi.setFinishTime());
return registeredHandle;
}
public static void NOPH_AsyncNet_printUrl(int c_connection)
{
function_info fi = new function_info("_NOPH_AsyncNet_printUrl");
AsyncNet.printUrl(c_connection);
fis.Add(fi.setFinishTime());
}
public static int NOPH_AsyncNet_read(int __async, int buffer, int len)
{
function_info fi = new function_info("NOPH_AsyncNet_read");
AsyncNet async = (AsyncNet)CRunTime.objectRepository[__async];
int ret = (int)async.read(buffer, len);
fis.Add(fi.setFinishTime());
return ret;
}
public static void NOPH_AsyncNet_startRead(int __async, int input_id)
{
function_info fi = new function_info("NOPH_AsyncNet_startRead");
AsyncNet async = (AsyncNet)CRunTime.objectRepository[__async];
async.startRead(input_id);
fis.Add(fi.setFinishTime());
}
public static void NOPH_AsyncNet_stopRead(int __async)
{
function_info fi = new function_info("NOPH_AsyncNet_stopRead");
AsyncNet async = (AsyncNet)CRunTime.objectRepository[__async];
async.stopRead();
fis.Add(fi.setFinishTime());
}
public static void NOPH_Bitmap_Waze_resetARGB(int c_bitmap, int width, int height)
{
function_info fi = new function_info("_NOPH_Bitmap_Waze_resetARGB");
fis.Add(fi.setFinishTime());
//todomt
// reset the transprancy of a bitmap
/*net.rim.device.api.system.Bitmap bitmap = (net.rim.device.api.system.Bitmap)CRunTime.getRegisteredObject(c_bitmap);
bitmap.createAlpha(net.rim.device.api.system.Bitmap.ALPHA_BITDEPTH_8BPP);
int dataTransparent[] = new int[width * height];
bitmap.setARGB(dataTransparent, 0, width, 0, 0, width, height);*/
}
public static int NOPH_Bitmap_getBitmapFromBytes(int buf, int size, int scale_factor)
{
function_info fi = new function_info("NOPH_Bitmap_getBitmapFromBytes");
//todomt
/* net.rim.device.api.system.Bitmap bitmap;
byte[] image = new byte[size];
int registeredHandle = 0;
CRunTime.memcpy(image,0,buf,size);
try{
bitmap = net.rim.device.api.system.Bitmap.createBitmapFromBytes(image,0,-1,scale_factor);
}catch(Exception e){
System.out.println(e.getMessage());
return 0;
}
registeredHandle = CRunTime.registerObject(bitmap);
return registeredHandle;*/
fis.Add(fi.setFinishTime());
return 1;
}
public static bool FileExists(string filename)
{
function_info fi = new function_info("FileExists");
Stream stream = GetFileStream(filename, FileMode.Open);
if (stream != null)
{
stream.Close();
fis.Add(fi.setFinishTime());
return true;
}
else
{
fis.Add(fi.setFinishTime());
return false;
}
}
private static ManualResetEvent mre = new ManualResetEvent(false);
private static int bitmap_registeredHandle;
public static int NOPH_Bitmap_getBitmapResource(int __name)
{
function_info fi = new function_info("NOPH_Bitmap_getBitmapResource");
String name = "/WazeWP7;component/resources/" + CRunTime.charPtrToString(__name);
if (!FileExists(name))
{
name = "Userstore://" + CRunTime.charPtrToString(__name);
if (!FileExists(name))
{
fis.Add(fi.setFinishTime());
return 0;
}
}
mre.Reset();
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
BitmapImage bitmap = new BitmapImage();
name = "/WazeWP7;component/resources/" + CRunTime.charPtrToString(__name);
Stream stream = GetFileStream(name, FileMode.Open);
bitmap.SetSource(stream);
bitmap_registeredHandle = CRunTime.registerObject(bitmap);
bitmaps_info.Add(bitmap_registeredHandle, new BitmapInfo(bitmap.PixelWidth, bitmap.PixelHeight));
mre.Set();
});
mre.WaitOne();
fis.Add(fi.setFinishTime());
return bitmap_registeredHandle;
}
private class BitmapInfo
{
public BitmapInfo(int width, int height)
{
this.width = width;
this.height = height;
}
public int width, height;
}
private static Dictionary<int, BitmapInfo> bitmaps_info = new Dictionary<int, BitmapInfo>();
//private static int bitmap_height;
public static int NOPH_Bitmap_getHeight(int __bitmap)
{
function_info fi = new function_info("NOPH_Bitmap_getHeight");
BitmapInfo dummy;
if (!bitmaps_info.TryGetValue(__bitmap, out dummy))
{
fis.Add(fi.setFinishTime());
return 0;
}
if (bitmaps_info[__bitmap].height == 0)
{
fis.Add(fi.setFinishTime());
return 0;
}
fis.Add(fi.setFinishTime());
return bitmaps_info[__bitmap].height;
/* mre.Reset();
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
BitmapImage bitmap = (BitmapImage)CRunTime.objectRepository[__bitmap];
bitmap_height = (int)bitmap.PixelHeight;
mre.Set();
});
mre.WaitOne();
return bitmap_height;*/
}
//private static int bitmap_width;
public static int NOPH_Bitmap_getWidth(int __bitmap)
{
function_info fi = new function_info("NOPH_Bitmap_getWidth");
BitmapInfo dummy;
if (!bitmaps_info.TryGetValue(__bitmap, out dummy))
{
fis.Add(fi.setFinishTime());
return 0;
}
if (bitmaps_info[__bitmap].width == 0)
{
fis.Add(fi.setFinishTime());
return 0;
}
fis.Add(fi.setFinishTime());
return bitmaps_info[__bitmap].width;
/* mre.Reset();
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
BitmapImage bitmap = (BitmapImage)CRunTime.objectRepository[__bitmap];
bitmap_width = (int)bitmap.PixelWidth;
mre.Set();
});
mre.WaitOne();
return bitmap_width;*/
}
private static BitmapImage bitmap_object;
public static int NOPH_Bitmap_new(int width, int height)
{
function_info fi = new function_info("NOPH_Bitmap_new");
mre.Reset();
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
bitmap_object = new BitmapImage();
mre.Set();
});
mre.WaitOne();
int registeredHandle = CRunTime.registerObject(bitmap_object);
bitmaps_info.Add(registeredHandle, new BitmapInfo(width, height));
fis.Add(fi.setFinishTime());
return registeredHandle;
}
private const int NOPH_Calendar_HOUR = 11;
private const int NOPH_Calendar_MINUTE = 12;
private const int OPH_Calendar_SECOND = 13;
public static int NOPH_Calendar_get(int __calendar, int what)
{
function_info fi = new function_info("NOPH_Calendar_get");
int ret = -1;
DateTime calendar = (DateTime)CRunTime.objectRepository[__calendar];
switch (what)
{
case NOPH_Calendar_HOUR:
ret = calendar.Hour;
break;
case NOPH_Calendar_MINUTE:
ret = calendar.Minute;
break;
case OPH_Calendar_SECOND:
ret = calendar.Second;
break;
}
fis.Add(fi.setFinishTime());
return ret;
}
public static int NOPH_Calendar_getInstance()
{
function_info fi = new function_info("NOPH_Calendar_getInstance");
DateTime ret = DateTime.Now;
int registeredHandle = CRunTime.registerObject(ret);
fis.Add(fi.setFinishTime());
return registeredHandle;
}
public static int NOPH_Canvas_get()
{
function_info fi = new function_info("NOPH_Canvas_get");
fis.Add(fi.setFinishTime());
return Syscalls.canvasHandle;
}
public static int NOPH_Class_getResourceAsStream(int __obj, int __name)
{
function_info fi = new function_info("NOPH_Class_getResourceAsStream");
Type obj = (Type)CRunTime.objectRepository[__obj];
String name = CRunTime.charPtrToString(__name);
Stream ret = GetFileStream(name, FileMode.Open);
if (ret == null)
{
ret = GetFileStream("userstore://" + name, FileMode.Open);
if (ret == null)
{
fis.Add(fi.setFinishTime());
return 0;
}
}
int registeredHandle = CRunTime.registerObject(ret);
fis.Add(fi.setFinishTime());
return registeredHandle;
}
public static void NOPH_ConfirmDialog_CreateDialog(int __title, int __text, int defaultYes, int __textYes, int __textNo, int seconds, int yesCallback, int noCallback, int context)
{
function_info fi = new function_info("NOPH_ConfirmDialog_CreateDialog");
String title = CRunTime.charPtrToString(__title);
String text = CRunTime.charPtrToString(__text);
String textYes = CRunTime.charPtrToString(__textYes);
String textNo = CRunTime.charPtrToString(__textNo);
mre.Reset();
System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
{
MessageBoxResult res = MessageBox.Show(text, title, MessageBoxButton.OKCancel);
if (res == MessageBoxResult.OK || res == MessageBoxResult.Yes)
{
UIWorker.addUIEvent(yesCallback, context, 0, 0, 0, true);
}
else
{
UIWorker.addUIEvent(noCallback, context, 0, 0, 0, true);
}
mre.Set();
});
mre.WaitOne();
fis.Add(fi.setFinishTime());
}
public static int NOPH_Connector_openDataInputStream(int __name)
{
function_info fi = new function_info("NOPH_Connector_openDataInputStream");
/* String name = CRunTime.charPtrToString(__name);
DataInputStream ret = (DataInputStream)Connector.openDataInputStream(name);
int registeredHandle = CRunTime.registerObject(ret);
return registeredHandle;*/
fis.Add(fi.setFinishTime());
return 1;
}
public static int NOPH_Connector_openDataOutputStream(int __name)
{
function_info fi = new function_info("NOPH_Connector_openDataOutputStream");
/* String name = CRunTime.charPtrToString(__name);
DataOutputStream ret = (DataOutputStream)Connector.openDataOutputStream(name);
int registeredHandle = CRunTime.registerObject(ret);
return registeredHandle;*/
fis.Add(fi.setFinishTime());
return 1;
}
public static int NOPH_Connector_openFileConnection_mode(int __name, int mode)
{
function_info fi = new function_info("NOPH_Connector_openFileConnection_mode");
String name = CRunTime.charPtrToString(__name);
FileConnection ret = new FileConnection(name, mode);
int registeredHandle = CRunTime.registerObject(ret);
fis.Add(fi.setFinishTime());
return registeredHandle;
}
public static int NOPH_DeviceInfo_getBatteryLevel()
{
function_info fi = new function_info("NOPH_DeviceInfo_getBatteryLevel");
fis.Add(fi.setFinishTime());
return 100;
}
public static void NOPH_DeviceSpecific_getPlatform(int addr, int size)
{
function_info fi = new function_info("NOPH_DeviceSpecific_getPlatform");
//todomt
String p = "WindowsPhone";
byte[] bytes = StringToAscii(p);
size--;
if (size > bytes.Length) size = bytes.Length;
CRunTime.memcpy(addr, bytes, 0, size);
CRunTime.memoryWriteByte(addr + size, 0);
fis.Add(fi.setFinishTime());
}
public static void NOPH_DeviceSpecific_init(int timeout, int isRefreshThread)
{
function_info fi = new function_info("NOPH_DeviceSpecific_init");
fis.Add(fi.setFinishTime());
/*todomt2
DeviceSpecific.init(timeout, isRefreshThread);*/
}
public static void NOPH_DeviceSpecific_setBacklightOn()
{
function_info fi = new function_info("NOPH_DeviceSpecific_setBacklightOn");
fis.Add(fi.setFinishTime());
/*todomt2
DeviceSpecific.setBacklightOn();
*/
}
static int textAddr = 0;
public static void NOPH_EditBoxScreen_registerEditBoxTextAddr(int addr)
{
function_info fi = new function_info("NOPH_EditBoxScreen_registerEditBoxTextAddr");
textAddr = addr;
fis.Add(fi.setFinishTime());
}
/*
public static int NOPH_Enumeration_hasMoreElements(int __enumeration)
{
Enumeration enumeration = (Enumeration)CRunTime.objectRepository[__enumeration];
boolean ret = (boolean)enumeration.hasMoreElements();
return ret ? 1 : 0;
}
public static int NOPH_Enumeration_nextElement(int __enumeration)
{
Enumeration enumeration = (Enumeration)CRunTime.objectRepository[__enumeration];
Object ret = (Object)enumeration.nextElement();
int registeredHandle = CRunTime.registerObject(ret);
return registeredHandle;
}*/
public static int NOPH_Exception_new()
{
function_info fi = new function_info("NOPH_Exception_new");
Exception ret = (Exception)new Exception();
int registeredHandle = CRunTime.registerObject(ret);
fis.Add(fi.setFinishTime());
return registeredHandle;
}
public static int NOPH_Exception_new_string(int __s)
{
function_info fi = new function_info("NOPH_Exception_new_string");
String s = CRunTime.charPtrToString(__s);
Exception ret = (Exception)new Exception(s);
int registeredHandle = CRunTime.registerObject(ret);
fis.Add(fi.setFinishTime());
return registeredHandle;
}
public static void NOPH_FileConnection_close(int __fc)
{
function_info fi = new function_info("NOPH_FileConnection_close");
FileConnection fc = (FileConnection)CRunTime.objectRepository[__fc];
fc.stream.Close();
fis.Add(fi.setFinishTime());
}
public static void NOPH_FileConnection_create(int __fc)
{
function_info fi = new function_info("NOPH_FileConnection_create");
FileConnection fc = (FileConnection)CRunTime.objectRepository[__fc];
Stream stream = GetFileStream(fc.filename, FileMode.OpenOrCreate);
fc.stream = stream;
stream.Close();
fis.Add(fi.setFinishTime());
}
public static void NOPH_FileConnection_delete(int __fc)
{
function_info fi = new function_info("NOPH_FileConnection_delete");
NOPH_FileConnection_truncate(__fc, 0);
fis.Add(fi.setFinishTime());
}
public static int NOPH_FileConnection_exists(int __fc)
{
function_info fi = new function_info("NOPH_FileConnection_exists");
FileConnection fc = (FileConnection)CRunTime.objectRepository[__fc];
Stream stream = GetFileStream(fc.filename, FileMode.Open);
int exist = (stream != null) ? 1 : 0;
if (stream != null)
{
stream.Close();
}
fis.Add(fi.setFinishTime());
return exist;
}
public static int NOPH_FileConnection_fileSize(int __fc)
{
function_info fi = new function_info("NOPH_FileConnection_fileSize");
string name;
FileType file_type;
FileConnection fc = (FileConnection)CRunTime.objectRepository[__fc];
GetFileTypeAndName(fc.filename, out file_type, out name);
if (file_type == FileType.USER_STORE)
{
var store = IsolatedStorageFile.GetUserStoreForApplication();
fis.Add(fi.setFinishTime());
return store.FileExists(name) ? 1 : 0;
}
else
{
fis.Add(fi.setFinishTime());
return (int)GetFileStream(fc.filename, FileMode.Open).Length;
}
}
public static int NOPH_FileConnection_openDataInputStream(int __fc)
{
function_info fi = new function_info("NOPH_FileConnection_openDataInputStream");
FileConnection fc = (FileConnection)CRunTime.objectRepository[__fc];
Stream stream = GetFileStream(fc.filename, FileMode.Open);
if (stream == null)
{
fis.Add(fi.setFinishTime());
return 0;
}
else
{
int registeredHandle = CRunTime.registerObject(stream);
fis.Add(fi.setFinishTime());
return registeredHandle;
}
}
private enum FileType
{
USER_STORE,
RESOURCE
}
private static void GetFileTypeAndName(string raw_name, out FileType file_type, out string filename)
{
function_info fi = new function_info("GetFileTypeAndName");
if (raw_name.ToLower().StartsWith("file:///userstore://"))
{
file_type = FileType.USER_STORE;
filename = raw_name.Substring(20);
}
else if (raw_name.ToLower().StartsWith("userstore://"))
{
file_type = FileType.USER_STORE;
filename = raw_name.Substring(12);
}
else if (raw_name.ToLower().StartsWith("file://"))
{
file_type = FileType.RESOURCE;
filename = raw_name.Substring(8);
}
else if (raw_name.ToLower().StartsWith("/wazewp7"))
{
file_type = FileType.RESOURCE;
filename = raw_name;
}
else if (raw_name.StartsWith("/"))
{
file_type = FileType.RESOURCE;
filename = "/WazeWP7;component/resources/" + raw_name.Substring(1);
}
else if (raw_name.Contains("resource:/"))
{
file_type = FileType.RESOURCE;
filename = "/WazeWP7;component/resources/" + raw_name.Substring(12);
}
else if (!raw_name.Contains(":"))
{
file_type = FileType.USER_STORE;
filename = raw_name;
}
else
{
fis.Add(fi.setFinishTime());
throw new ArgumentException("invalid raw_file: " + raw_name);
}
fis.Add(fi.setFinishTime());
}
public static Stream GetFileStream(string filename, FileMode mode)
{
function_info fi = new function_info("GetFileStream");
string name;
FileType file_type;
GetFileTypeAndName(filename, out file_type, out name);
if (file_type == FileType.RESOURCE)
{
var res = App.GetResourceStream(new Uri(name, UriKind.Relative));
if (res == null)
{
fis.Add(fi.setFinishTime());
return null;
}
fis.Add(fi.setFinishTime());
return res.Stream;
}
else if (file_type == FileType.USER_STORE)
{
if (name.StartsWith("/"))
name = name.Substring(1);
name = name.Replace("/", "\\");
var store = IsolatedStorageFile.GetUserStoreForApplication();
try
{
if (!store.DirectoryExists("skins"))
store.CreateDirectory("skins");
if (!store.DirectoryExists("skins\\default"))
store.CreateDirectory("skins\\default");
if (store.FileExists(name) || mode == FileMode.OpenOrCreate)
{
fis.Add(fi.setFinishTime());
return new IsolatedStorageFileStream(name, mode, store);
}
else
{
fis.Add(fi.setFinishTime());
return null;
}
}
catch (IsolatedStorageException ise)
{
fis.Add(fi.setFinishTime());
return null;
}
catch (FileNotFoundException fnf)
{
fis.Add(fi.setFinishTime());
return null;
}
}
fis.Add(fi.setFinishTime());
return null;
}
public static int NOPH_FileConnection_openInputStream(int __fc)
{
function_info fi = new function_info("NOPH_FileConnection_openInputStream");
FileConnection fc = (FileConnection)CRunTime.objectRepository[__fc];
if (File.Exists(fc.filename))
{
FileStream ret = new FileStream(fc.filename, FileMode.Open, FileAccess.Read);
fc.stream = ret;
int registeredHandle = CRunTime.registerObject(ret);
fis.Add(fi.setFinishTime());
return registeredHandle;
}
else
{
fis.Add(fi.setFinishTime());
return 0;
}
}
public static int NOPH_FileConnection_openOutputStream(int __fc, int offset)
{
function_info fi = new function_info("NOPH_FileConnection_openOutputStream");
FileConnection fc = (FileConnection)CRunTime.objectRepository[__fc];
Stream ret = GetFileStream(fc.filename, FileMode.OpenOrCreate);
fc.stream = ret;
int registeredHandle = CRunTime.registerObject(ret);
fis.Add(fi.setFinishTime());
return registeredHandle;
}
public static void NOPH_FileConnection_truncate(int __fc, int byteOffest)
{
function_info fi = new function_info("NOPH_FileConnection_truncate");
FileConnection fc = (FileConnection)CRunTime.objectRepository[__fc];
if (fc.filename.ToLower().StartsWith("userstore://"))
{
var store = IsolatedStorageFile.GetUserStoreForApplication();
store.DeleteFile(fc.filename.Substring(12));
}
else
{
fis.Add(fi.setFinishTime());
return;
}
fis.Add(fi.setFinishTime());
}
/*
public static int NOPH_FileConnection_list(int __fc)
{
FileConnection fc = (FileConnection)CRunTime.objectRepository[__fc];
Enumeration ret = (Enumeration)fc.list();
int registeredHandle = CRunTime.registerObject(ret);
return registeredHandle;
}
public static void NOPH_FileConnection_mkdir(int __fc)
{
FileConnection fc = (FileConnection)CRunTime.objectRepository[__fc];
fc.mkdir();
}*/
public static int NOPH_Font_derive(int __font, int style, int height)
{
function_info fi = new function_info("NOPH_Font_derive");
fis.Add(fi.setFinishTime());
return height;
/* object font = new object();
//todomt
//Font font = (Font)CRunTime.objectRepository[__font];
// Font ret = (Font)font.derive(style, height);
int registeredHandle = CRunTime.registerObject(font);
return registeredHandle;*/
}
public static int NOPH_Font_getAdvance(int __font, int __text)
{
function_info fi = new function_info("NOPH_Font_getAdvance");
int charwidth;
switch (__font)
{
case 7:
case 8:
charwidth = 5;
break;
case 9:
case 10:
charwidth = 6;
break;
case 11:
charwidth = 7;
break;
case 12:
case 13:
charwidth = 8;
break;