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