root/lang/csharp/JSDokan/Program.cs @ 13055

Revision 13055, 12.1 kB (checked in by nitoyon, 5 years ago)

initial import

Line 
1using System;
2using System.IO;
3using System.Collections.Generic;
4using System.Text;
5using Microsoft.Win32;
6using Dokan;
7using System.Reflection;
8using System.CodeDom.Compiler;
9using Microsoft.JScript;
10
11namespace JSDokan
12{
13    class JSDokan : DokanOperations
14    {
15        #region DokanOperations �����o
16
17        private object instance;
18        private Type type;
19
20        public JSDokan(object instance, Type t)
21        {
22            this.instance = instance;
23            this.type = t;
24        }
25
26        public int Cleanup(string filename, DokanFileInfo info)
27        {
28            return (type.GetMethod("Cleanup") != null ?
29                (int)type.InvokeMember("Cleanup",
30                    BindingFlags.InvokeMethod,
31                    null,
32                    instance,
33                    new object[] { filename, info }) :
34                0);
35        }
36
37        public int CloseFile(string filename, DokanFileInfo info)
38        {
39            return (type.GetMethod("CloseFile") != null ?
40                (int)type.InvokeMember("CloseFile",
41                    BindingFlags.InvokeMethod,
42                    null,
43                    instance,
44                    new object[] { filename, info }) :
45                0);
46        }
47
48        public int CreateDirectory(string filename, DokanFileInfo info)
49        {
50            return (type.GetMethod("CreateDirectory") != null ?
51                (int)type.InvokeMember("CreateDirectory",
52                    BindingFlags.InvokeMethod,
53                    null,
54                    instance,
55                    new object[] { filename, info }) :
56                -1);
57        }
58
59        public int CreateFile(
60            string filename,
61            System.IO.FileAccess access,
62            System.IO.FileShare share,
63            System.IO.FileMode mode,
64            System.IO.FileOptions options,
65            DokanFileInfo info)
66        {
67            return (type.GetMethod("CreateFile") != null ?
68                (int)type.InvokeMember("CreateFile",
69                    BindingFlags.InvokeMethod,
70                    null,
71                    instance,
72                    new object[] { filename, access, share, mode, options, info }) :
73                0);
74        }
75
76        public int DeleteDirectory(string filename, DokanFileInfo info)
77        {
78            return (type.GetMethod("DeleteDirectory") != null ?
79                (int)type.InvokeMember("DeleteDirectory",
80                    BindingFlags.InvokeMethod,
81                    null,
82                    instance,
83                    new object[] { filename, info }) :
84                -1);
85        }
86
87        public int DeleteFile(string filename, DokanFileInfo info)
88        {
89            return (type.GetMethod("DeleteFile") != null ?
90                (int)type.InvokeMember("DeleteFile",
91                    BindingFlags.InvokeMethod,
92                    null,
93                    instance,
94                    new object[] { filename, info }) :
95                -1);
96        }
97
98
99        public int FlushFileBuffers(
100            string filename,
101            DokanFileInfo info)
102        {
103            return (type.GetMethod("FlushFileBuffers") != null ?
104                (int)type.InvokeMember("FlushFileBuffers",
105                    BindingFlags.InvokeMethod,
106                    null,
107                    instance,
108                    new object[] { filename, info }) :
109                -1);
110        }
111
112        public int FindFiles(
113            string filename,
114            System.Collections.ArrayList files,
115            DokanFileInfo info)
116        {
117            return (type.GetMethod("FindFiles") != null ?
118                (int)type.InvokeMember("FindFiles",
119                    BindingFlags.InvokeMethod,
120                    null,
121                    instance,
122                    new object[] {filename, files, info }) :
123                -1);
124        }
125
126
127        public int GetFileInformation(
128            string filename,
129            FileInformation fileinfo,
130            DokanFileInfo info)
131        {
132            return (type.GetMethod("GetFileInformation") != null ?
133                (int)type.InvokeMember("GetFileInformation",
134                    BindingFlags.InvokeMethod,
135                    null,
136                    instance,
137                    new object[] { filename, fileinfo, info }) :
138                -1);
139        }
140
141        public int LockFile(
142            string filename,
143            long offset,
144            long length,
145            DokanFileInfo info)
146        {
147            return (type.GetMethod("LockFile") != null ?
148                (int)type.InvokeMember("LockFile",
149                    BindingFlags.InvokeMethod,
150                    null,
151                    instance,
152                    new object[] { filename, offset, length, info }) :
153                0);
154        }
155
156        public int MoveFile(
157            string filename,
158            string newname,
159            bool replace,
160            DokanFileInfo info)
161        {
162            return (type.GetMethod("MoveFile") != null ?
163                (int)type.InvokeMember("MoveFile",
164                    BindingFlags.InvokeMethod,
165                    null,
166                    instance,
167                    new object[] { filename, newname, replace, info }) :
168                -1);
169        }
170
171        public int OpenDirectory(string filename, DokanFileInfo info)
172        {
173            return (type.GetMethod("OpenDirectory") != null ?
174                (int)type.InvokeMember("OpenDirectory",
175                    BindingFlags.InvokeMethod,
176                    null,
177                    instance,
178                    new object[] { filename, info }) :
179                0);
180        }
181
182        public int ReadFile(
183            string filename,
184            byte[] buffer,
185            ref uint readBytes,
186            long offset,
187            DokanFileInfo info)
188        {
189            uint[] refReadBytes = new uint[] { readBytes };
190            int ret = (type.GetMethod("ReadFile") != null ?
191                (int)type.InvokeMember("ReadFile",
192                    BindingFlags.InvokeMethod,
193                    null,
194                    instance,
195                    new object[] { filename, buffer, refReadBytes, offset, info }) : //, offset, info }) :
196                -1);
197            readBytes = refReadBytes[0];
198            return ret;
199        }
200
201        public int SetEndOfFile(string filename, long length, DokanFileInfo info)
202        {
203            return (type.GetMethod("SetEndOfFile") != null ?
204                (int)type.InvokeMember("SetEndOfFile",
205                    BindingFlags.InvokeMethod,
206                    null,
207                    instance,
208                    new object[] { filename, length, info }) :
209                -1);
210        }
211
212        public int SetFileAttributes(
213            string filename,
214            System.IO.FileAttributes attr,
215            DokanFileInfo info)
216        {
217            return (type.GetMethod("SetFileAttributes") != null ?
218                (int)type.InvokeMember("SetFileAttributes",
219                    BindingFlags.InvokeMethod,
220                    null,
221                    instance,
222                    new object[] { filename, attr, info }) :
223                -1);
224        }
225
226        public int SetFileTime(
227            string filename,
228            DateTime ctime,
229            DateTime atime,
230            DateTime mtime,
231            DokanFileInfo info)
232        {
233            return (type.GetMethod("SetFileTime") != null ?
234                (int)type.InvokeMember("SetFileTime",
235                    BindingFlags.InvokeMethod,
236                    null,
237                    instance,
238                    new object[] { filename, ctime, atime, mtime, info }) :
239                -1);
240        }
241
242        public int UnlockFile(string filename, long offset, long length, DokanFileInfo info)
243        {
244            return (type.GetMethod("UnlockFile") != null ?
245                (int)type.InvokeMember("UnlockFile",
246                    BindingFlags.InvokeMethod,
247                    null,
248                    instance,
249                    new object[] { filename, offset, length, info }) :
250                0);
251        }
252
253        public int Unmount(DokanFileInfo info)
254        {
255            return (type.GetMethod("Unmount") != null ?
256                (int)type.InvokeMember("Unmount",
257                    BindingFlags.InvokeMethod,
258                    null,
259                    instance,
260                    new object[] { info }) :
261                0);
262        }
263
264        public int GetDiskFreeSpace(
265           ref ulong freeBytesAvailable,
266           ref ulong totalBytes,
267           ref ulong totalFreeBytes,
268           DokanFileInfo info)
269        {
270            ulong[] refBytes = new ulong[] { freeBytesAvailable, totalBytes, totalFreeBytes };
271
272            int ret = (type.GetMethod("GetDiskFreeSpace") != null ?
273                (int)type.InvokeMember("GetDiskFreeSpace",
274                    BindingFlags.InvokeMethod,
275                    null,
276                    instance,
277                    new object[] { refBytes, info }) :
278                -1);
279
280            freeBytesAvailable = refBytes[0];
281            totalBytes = refBytes[1];
282            totalFreeBytes = refBytes[2];
283            return ret;
284        }
285
286        public int WriteFile(
287            string filename,
288            byte[] buffer,
289            ref uint writtenBytes,
290            long offset,
291            DokanFileInfo info)
292        {
293            uint[] refWrittenBytes = new uint[] { writtenBytes };
294
295            int ret = (type.GetMethod("WriteFile") != null ?
296                (int)type.InvokeMember("WriteFile",
297                    BindingFlags.InvokeMethod,
298                    null,
299                    instance,
300                    new object[] { filename, buffer, refWrittenBytes, offset, info }) :
301                -1);
302
303            writtenBytes = refWrittenBytes[0];
304            return ret;
305        }
306
307        #endregion
308    }
309
310    class Program
311    {
312        static void Main(string[] args)
313        {
314            new Program(args);
315        }
316
317        Program(string[] args)
318        {
319            if (args.Length != 2)
320            {
321                Usage();
322                return;
323            }
324
325            Type t = CompileJS(args[0]);
326            if (t == null) return;
327
328            object instance = Activator.CreateInstance(t);
329
330            DokanOptions opt = new DokanOptions();
331            opt.DriveLetter = args[1][0];
332            opt.DebugMode = false;// true;
333            opt.UseStdErr = false;// true;
334            opt.VolumeLabel = "JSDokan";
335            DokanNet.DokanMain(opt, new JSDokan(instance, t));
336        }
337
338        void Usage()
339        {
340            Console.WriteLine("JSDokan.exe [script path] [drive letter]");
341            Console.WriteLine("  (ex) JSDokan.exe sample.js J");
342        }
343
344        Type CompileJS(String filename)
345        {
346            //�R���p�C�����邽�߂̏���           CodeDomProvider cp = new Microsoft.JScript.JScriptCodeProvider();
347            ICodeCompiler icc = cp.CreateCompiler();
348            CompilerParameters cps = new CompilerParameters();
349            cps.GenerateInMemory = true;//���������o�͂𐶐�����            cps.ReferencedAssemblies.Add("DokanNet.dll");
350
351            CompilerResults cres;
352           
353            //�R���p�C������            cres = icc.CompileAssemblyFromFile(cps, filename);
354            if (cres.NativeCompilerReturnValue != 0)
355            {
356                foreach (String s in cres.Output)
357                {
358                    Console.WriteLine(s);
359                }
360                return null;
361            }
362
363            //�R���p�C�������A�Z���u�����
364            Assembly asm = cres.CompiledAssembly;
365            Type t = asm.GetType(Path.GetFileNameWithoutExtension(filename));
366            if (t == null)
367            {
368                Console.WriteLine("Class {0} not found", Path.GetFileNameWithoutExtension(filename));
369            }
370            return t;
371        }
372    }
373}
Note: See TracBrowser for help on using the browser.