Skip to content

Commit be5b442

Browse files
committed
chore: Organize util modules
1 parent 4e6fd73 commit be5b442

File tree

3 files changed

+55
-4
lines changed

3 files changed

+55
-4
lines changed

Assets/JCSUnity/Scripts/IO/JCS_IO.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,56 @@ public static bool CreateDirectory(string path)
119119
Directory.CreateDirectory(path);
120120
return true;
121121
}
122+
123+
/// <summary>
124+
/// Method to do search directory and get the last file index.
125+
/// </summary>
126+
/// <param name="path"> path to search index. </param>
127+
/// <param name="prefixStr"> Filen name prefix. </param>
128+
/// <param name="ext"> Filen name extension. </param>
129+
/// <returns></returns>
130+
public static int LastFileIndex(string path, string prefixStr, string ext)
131+
{
132+
JCS_IO.CreateDirectory(path);
133+
134+
var gs = JCS_GameSettings.instance;
135+
136+
string fileName = "";
137+
string curExt = "";
138+
int last_saved_screenshot = -1;
139+
140+
foreach (string file in Directory.GetFiles(path))
141+
{
142+
fileName = Path.GetFileNameWithoutExtension(file);
143+
curExt = Path.GetExtension(file);
144+
145+
// check if is the .png file
146+
// (screen shot can only be image file)
147+
if (!curExt.Equals(ext))
148+
continue;
149+
150+
int index = fileName.IndexOf(prefixStr);
151+
int len = prefixStr.Length;
152+
string startOfString = fileName.Substring(0, index);
153+
string endOfString = fileName.Substring(index + len);
154+
string cleanPath = startOfString + endOfString;
155+
156+
last_saved_screenshot = System.Int32.Parse(cleanPath);
157+
}
158+
159+
return last_saved_screenshot;
160+
}
161+
162+
/// <summary>
163+
/// Delete all files in directory.
164+
/// </summary>
165+
/// <param name="dirPath"> Target delete directory. </param>
166+
public static void DeleteAllFilesFromDir(string dirPath)
167+
{
168+
DirectoryInfo di = new DirectoryInfo(dirPath);
169+
170+
foreach (FileInfo file in di.GetFiles())
171+
file.Delete();
172+
}
122173
}
123174
}

Assets/JCSUnity/Scripts/JCS_Camera.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public static int LastImageFileIndex()
179179
var gs = JCS_GameSettings.instance;
180180
var prefix = gs.SCREENSHOT_FILENAME;
181181
var ext = gs.SCREENSHOT_EXTENSION;
182-
return JCS_Util.LastFileIndex(SavePath(), prefix, ext);
182+
return JCS_IO.LastFileIndex(SavePath(), prefix, ext);
183183
}
184184

185185
/// <summary>
@@ -243,7 +243,7 @@ public static void DeleteImageByIndex(int index)
243243
/// </summary>
244244
public static void DeleteAllImages()
245245
{
246-
JCS_Util.DeleteAllFilesFromDir(SavePath());
246+
JCS_IO.DeleteAllFilesFromDir(SavePath());
247247
}
248248

249249
private Vector3 GameDepthRect(Vector3 depth)

Assets/JCSUnity/Scripts/Webcam/JCS_Webcam.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ public static int LastImageFileIndex()
260260
var gs = JCS_GameSettings.instance;
261261
var prefix = gs.WEBCAM_FILENAME;
262262
var ext = gs.WEBCAM_EXTENSION;
263-
return JCS_Util.LastFileIndex(SavePath(), prefix, ext);
263+
return JCS_IO.LastFileIndex(SavePath(), prefix, ext);
264264
}
265265

266266
/// <summary>
@@ -324,7 +324,7 @@ public static void DeleteImageByIndex(int index)
324324
/// </summary>
325325
public static void DeleteAllImages()
326326
{
327-
JCS_Util.DeleteAllFilesFromDir(SavePath());
327+
JCS_IO.DeleteAllFilesFromDir(SavePath());
328328
}
329329

330330
/// <summary>

0 commit comments

Comments
 (0)