Documentation for the Unity C# Library
Loading...
Searching...
No Matches
PixoWindowsPlatformUtilities.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.IO;
5using UnityEngine;
6using UDebug = UnityEngine.Debug;
7
8namespace PixoVR.Apex
9{
11 {
13 {
14 UDebug.Log($"Initializing class {GetType().Name}");
15 }
16
17 public override bool OpenURL(string url)
18 {
19 UDebug.Log($"[{GetType().Name}] Opening url {url}");
20 if (string.IsNullOrEmpty(url))
21 {
22 UDebug.Log("Url is empty or null.");
23 return false;
24 }
25
26 Application.OpenURL(url);
27 Application.Quit();
28 return true;
29 }
30
31 public override bool OpenApplication(string applicationPath, string[] argumentKeys, string[] argumentValues)
32 {
33 if (!string.IsNullOrEmpty(applicationPath))
34 {
35 UDebug.Log("Application is empty.");
36 return false;
37 }
38
39 if (!File.Exists(applicationPath))
40 {
41 UDebug.Log($"Application does not exist at {applicationPath}");
42 return false;
43 }
44
45 int argumentCount = Mathf.Max(argumentKeys.Length, argumentValues.Length);
46 if (argumentKeys.Length != argumentValues.Length)
47 {
48 UDebug.LogWarning("The number of argument keys and values are not equal. Extra arguments will not be provided and mapping could be messed up.");
49 }
50
51 string arguments = "";
52 for (int argumentIndex = 0; argumentIndex < argumentCount; argumentIndex++)
53 {
54 if (argumentIndex > 0)
55 arguments += " ";
56
57 // We use space here between the key and value as most OS's that are launched with arguments separate by spaces.
58 // We also escape our values to ensure that strings containing spaces get captured.
59 arguments += $"-{argumentKeys[argumentIndex]} \"{argumentValues[argumentIndex]}\"";
60 }
61
62 using (Process process = new Process())
63 {
64 process.StartInfo.WorkingDirectory = applicationPath;
65 process.StartInfo.Arguments = arguments;
66 process.StartInfo.FileName = applicationPath;
67 process.Start();
68 }
69
70 return true;
71 }
72
73 public override Dictionary<string, string> ParseApplicationArguments()
74 {
75#if UNITY_EDITOR
76 // We don't want to parse any parameters in the editor as it has its own list of arguments.
77 return new Dictionary<string, string>();
78#else
79 string[] args = System.Environment.GetCommandLineArgs();
80
81 UDebug.Log($"First argument: {args[0]}");
82
83 if(args.Length == 2)
84 {
85 string urlData = args[1];
86 UDebug.Log("[PixoWindowsPlatformUtilities] Parse from URL.");
87 return ParseURLArguments(urlData);
88 }
89
90 UDebug.Log("[PixoWindowsPlatformUtilities] Parsing arguments from commandline.");
91 Dictionary<string, string> parameters = new Dictionary<string, string>();
92
93 // We skip the first argument, as it's the executables name.
94 for(int argumentIndex = 1; argumentIndex < args.Length; argumentIndex++)
95 {
96 if (args[argumentIndex].StartsWith('-') && ((argumentIndex + 1) < args.Length))
97 {
98 parameters.Add(args[argumentIndex].Remove(0), args[argumentIndex + 1]);
99 argumentIndex++;
100 }
101 }
102
103 return parameters;
104#endif
105 }
106
107 public override bool ReadFileAsString(string fileName, out string data)
108 {
109 if (string.IsNullOrEmpty(fileName))
110 {
111 UDebug.LogError("File name is null or empty.");
112 data = null;
113 return false;
114 }
115
116 string path = Path.Combine(Application.persistentDataPath, fileName);
117
118 try
119 {
120 if (!File.Exists(path))
121 {
122 UDebug.LogError($"File not found at path: {path}");
123 data = null;
124 return false;
125 }
126
127 data = File.ReadAllText(path);
128 return true;
129 }
130 catch (Exception ex)
131 {
132 UDebug.LogError($"Failed to read file: {path}\nException: {ex.Message}");
133 data = null;
134 return false;
135 }
136 }
137
138 public override bool ReadFile(string fileName, out byte[] data)
139 {
140 if (string.IsNullOrEmpty(fileName))
141 {
142 UDebug.LogError("File name is null or empty.");
143 data = null;
144 return false;
145 }
146
147 string path = Path.Combine(Application.persistentDataPath, fileName);
148
149 try
150 {
151 if (!File.Exists(path))
152 {
153 UDebug.LogError($"File not found at path: {path}");
154 data = null;
155 return false;
156 }
157
158 data = File.ReadAllBytes(path);
159 return true;
160 }
161 catch (Exception ex)
162 {
163 UDebug.LogError($"Failed to read file: {path}\nException: {ex.Message}");
164 data = null;
165 return false;
166 }
167 }
168
169 public override bool WriteFile(string fileName, byte[] data)
170 {
171 if (string.IsNullOrEmpty(fileName))
172 {
173 UDebug.LogError("File name is null or empty.");
174 data = null;
175 return false;
176 }
177
178 if (data == null || data.Length == 0)
179 {
180 UDebug.LogError("No data provided to write.");
181 return false;
182 }
183
184 string path = Path.Combine(Application.persistentDataPath, fileName);
185
186 try
187 {
188 File.WriteAllBytes(path, data);
189 UDebug.Log($"File saved successfully at: {path}");
190 return true;
191 }
192 catch (Exception ex)
193 {
194 UDebug.LogError($"Failed to write file at {path}:\n{ex.Message}");
195 return false;
196 }
197 }
198
199 public override bool WriteStringToFile(string fileName, string data, System.Text.Encoding encoding = null)
200 {
201 if (string.IsNullOrEmpty(fileName))
202 {
203 UDebug.LogError("File name is null or empty.");
204 data = null;
205 return false;
206 }
207
208 if (data == null || data.Length == 0)
209 {
210 UDebug.LogError("No data provided to write.");
211 return false;
212 }
213
214 string path = Path.Combine(Application.persistentDataPath, fileName);
215
216 try
217 {
218 if(encoding == null)
219 {
220 File.WriteAllText(path, data);
221 }
222 else
223 {
224 File.WriteAllText(path, data, encoding);
225 }
226
227 UDebug.Log($"File saved successfully at: {path}");
228 return true;
229 }
230 catch (Exception ex)
231 {
232 UDebug.LogError($"Failed to write file at {path}:\n{ex.Message}");
233 return false;
234 }
235 }
236 }
237}
UnityEngine.Debug UDebug
virtual Dictionary< string, string > ParseURLArguments(string url)
override bool WriteStringToFile(string fileName, string data, System.Text.Encoding encoding=null)
override Dictionary< string, string > ParseApplicationArguments()
override bool ReadFileAsString(string fileName, out string data)
override bool ReadFile(string fileName, out byte[] data)
override bool WriteFile(string fileName, byte[] data)
override bool OpenApplication(string applicationPath, string[] argumentKeys, string[] argumentValues)