2class SCR_WorldFilesHelper
5 protected static const string GENERIC_WORLD_ENTITY_CLASS =
"GenericWorldEntity";
6 protected static const string LAYERS_DIR =
"%1_Layers";
7 protected static const string DEFAULT_LAYER =
"default.layer";
9 protected static const ref array<string> COPY_SUB_DIRECTORIES = {
".Rivers",
".Shore",
"SurfaceMasks",
"Terrain",
"%1", LAYERS_DIR };
11 protected static const ref array<string> COPY_DIR_FILES = {
"%1.ent",
"%1.smap",
"%1.smd",
"%1.topo",
"%1_SMD.conf" };
12 protected static const ref array<bool> COPY_DIR_FILES_REGISTER = { 1, 1, 0, 1, 1 };
22 static string CreateWorld(
string relativeWorldDirectory,
string worldName, array<ResourceName> defaultLayerEntities = null,
bool overwriteWorldFile =
false,
bool overwriteDefaultLayerFile =
false)
24 if (SCR_StringHelper.IsEmptyOrWhiteSpace(relativeWorldDirectory))
30 if (SCR_StringHelper.IsEmptyOrWhiteSpace(worldName))
36 string absoluteWorldDirectory;
37 if (!Workbench.GetAbsolutePath(relativeWorldDirectory, absoluteWorldDirectory,
false))
39 Print(
"Wrong relative world directory provided: " + relativeWorldDirectory,
LogLevel.WARNING);
43 string worldFilePath = FilePath.Concat(absoluteWorldDirectory, worldName +
".ent");
44 string layersDirectory = FilePath.Concat(absoluteWorldDirectory, worldName +
"_Layers");
45 string defaultLayerFilePath = FilePath.Concat(layersDirectory,
"default.layer");
47 if (!FileIO.FileExists(absoluteWorldDirectory))
48 FileIO.MakeDirectory(absoluteWorldDirectory);
51 if (!FileIO.FileExists(layersDirectory))
53 if (!FileIO.MakeDirectory(layersDirectory))
61 if (overwriteWorldFile || !FileIO.FileExists(worldFilePath))
63 FileHandle fileHandle = FileIO.OpenFile(worldFilePath,
FileMode.WRITE);
66 Print(
"Cannot write world file " + worldFilePath,
LogLevel.WARNING);
70 fileHandle.WriteLine(
"Layer default {");
71 fileHandle.WriteLine(
" Index 0");
72 fileHandle.WriteLine(
"}");
77 Print(
"File already exists, skipping " + worldFilePath,
LogLevel.WARNING);
81 if (overwriteDefaultLayerFile || !FileIO.FileExists(defaultLayerFilePath))
83 FileHandle fileHandle = FileIO.OpenFile(defaultLayerFilePath,
FileMode.WRITE);
86 Print(
"Cannot write default layer file " + defaultLayerFilePath,
LogLevel.WARNING);
90 ResourceName foundWorldEntity;
91 if (defaultLayerEntities)
93 foreach (ResourceName
resourceName : defaultLayerEntities)
95 if (SCR_BaseContainerTools.GetContainerClassName(
resourceName) == GENERIC_WORLD_ENTITY_CLASS)
104 if (foundWorldEntity.IsEmpty())
106 fileHandle.WriteLine(GENERIC_WORLD_ENTITY_CLASS +
" world {");
107 fileHandle.WriteLine(
"}");
111 fileHandle.WriteLine(GENERIC_WORLD_ENTITY_CLASS +
" world : \"" + foundWorldEntity +
"\" {");
112 fileHandle.WriteLine(
"}");
115 if (defaultLayerEntities)
117 foreach (ResourceName
resourceName : defaultLayerEntities)
122 fileHandle.WriteLine(
string.Format(
"%1 : \"%2\" {", SCR_BaseContainerTools.GetContainerClassName(
resourceName),
resourceName));
123 fileHandle.WriteLine(
"}");
131 Print(
"File already exists, skipping " + defaultLayerFilePath,
LogLevel.WARNING);
134 return relativeWorldDirectory +
"/" + worldName +
".ent";
142 static bool DuplicateWorld(
string absoluteSourceFilePath,
string absoluteDestinationFilePath)
145 PrintFormat(
"Duplicating %1 to %2", absoluteSourceFilePath, absoluteDestinationFilePath, level:
LogLevel.NORMAL);
147 ResourceManager resourceManager = Workbench.GetModule(ResourceManager);
149 if (SCR_StringHelper.IsEmptyOrWhiteSpace(absoluteSourceFilePath) || !absoluteSourceFilePath.EndsWith(
".ent"))
151 Print(
"Provided source world file is empty",
LogLevel.WARNING);
155 if (SCR_StringHelper.IsEmptyOrWhiteSpace(absoluteDestinationFilePath) || !absoluteDestinationFilePath.EndsWith(
".ent"))
157 Print(
"Provided destination world file is empty",
LogLevel.WARNING);
161 if (!FileIO.FileExists(absoluteSourceFilePath))
163 Print(
"Provided source world file does not exist",
LogLevel.WARNING);
167 if (FileIO.FileExists(absoluteDestinationFilePath))
169 Print(
"Provided destination world file exists",
LogLevel.WARNING);
173 string absoluteSourceDirPath = FilePath.StripFileName(absoluteSourceFilePath);
174 string absoluteDestinationDirPath = FilePath.StripFileName(absoluteDestinationFilePath);
176 if (!FileIO.FileExists(FilePath.Concat(absoluteSourceDirPath,
"Terrain")))
178 Print(
"The 'Terrain' subdirectory does not exist in the source world's directory",
LogLevel.WARNING);
182 string sourceWorldName = FilePath.StripExtension(FilePath.StripPath(absoluteSourceFilePath));
183 string destinationWorldName = FilePath.StripExtension(FilePath.StripPath(absoluteDestinationFilePath));
185 foreach (
int fileIndex,
string dirFile : COPY_DIR_FILES)
187 string sourceDirFile = FilePath.Concat(absoluteSourceDirPath,
string.Format(dirFile, sourceWorldName));
188 if (!FileIO.FileExists(sourceDirFile))
190 Print(sourceDirFile +
" not found, skipping",
LogLevel.NORMAL);
194 string destinationDirFile = FilePath.Concat(absoluteDestinationDirPath,
string.Format(dirFile, destinationWorldName));
196 if (!SCR_FileIOHelper.CopyFile(sourceDirFile, destinationDirFile))
198 PrintFormat(
"Cannot copy %1 to %2", sourceDirFile, destinationDirFile, level:
LogLevel.ERROR);
202 PrintFormat(
"Copied %1 to %2",
string.Format(dirFile, sourceWorldName),
string.Format(dirFile, destinationWorldName), level:
LogLevel.NORMAL);
204 if (COPY_DIR_FILES_REGISTER[fileIndex])
206 Print(
"Re-registering " + destinationDirFile,
LogLevel.NORMAL);
207 resourceManager.RegisterResourceFile(destinationDirFile,
false);
211 foreach (
string subDirectory : COPY_SUB_DIRECTORIES)
213 string sourceSubDir = FilePath.Concat(absoluteSourceDirPath,
string.Format(subDirectory, sourceWorldName));
214 if (!FileIO.FileExists(sourceSubDir))
216 Print(sourceSubDir +
" not found, skipping",
LogLevel.NORMAL);
220 string destinationSubDir = FilePath.Concat(absoluteDestinationDirPath,
string.Format(subDirectory, destinationWorldName));
221 if (!SCR_FileIOHelper.CopyDirectory(sourceSubDir, destinationSubDir))
227 PrintFormat(
"Copied %1 to %2",
string.Format(subDirectory, sourceWorldName),
string.Format(subDirectory, destinationWorldName), level:
LogLevel.NORMAL);
229 ReRegisterMetaFiles(resourceManager, destinationSubDir);
239 protected static void ReRegisterMetaFiles(notnull ResourceManager resourceManager,
string dir)
241 array<string> filesToRegister = {};
242 array<ref SCR_FileInfo> metaFiles = SCR_FileIOHelper.GetDirectoryContent(dir,
"meta");
243 foreach (SCR_FileInfo fileInfo : metaFiles)
249 if (!FileIO.DeleteFile(fileInfo.m_sFilePath))
251 Print(
"Cannot delete " + fileInfo.m_sFilePath,
LogLevel.ERROR);
255 string fileFromMeta = FilePath.StripExtension(fileInfo.m_sFilePath);
256 if (FileIO.FileExists(fileFromMeta))
257 filesToRegister.Insert(fileFromMeta);
260 foreach (
string fileToRegister : filesToRegister)
263 resourceManager.RegisterResourceFile(fileToRegister,
false);
272 static bool ReplaceWorldEntity(
string relativeWorldFilePath, ResourceName newWorldEntityPrefab)
274 if (!relativeWorldFilePath)
280 if (!newWorldEntityPrefab)
281 newWorldEntityPrefab = GENERIC_WORLD_ENTITY_CLASS;
283 string absoluteWorldFilePath;
284 if (!Workbench.GetAbsolutePath(relativeWorldFilePath, absoluteWorldFilePath,
true))
286 Print(
"Provided world file does not exist: " + relativeWorldFilePath,
LogLevel.WARNING);
290 string defaultLayerAbsFilePath = GetDefaultLayerAbsoluteFilePath(relativeWorldFilePath);
291 if (!defaultLayerAbsFilePath || !FileIO.FileExists(defaultLayerAbsFilePath))
293 Print(
"Cannot find default layer's location: \"" + defaultLayerAbsFilePath +
"\"",
LogLevel.WARNING);
297 Print(
"Reading " + defaultLayerAbsFilePath,
LogLevel.NORMAL);
298 array<string> lines = SCR_FileIOHelper.ReadFileContent(defaultLayerAbsFilePath);
301 for (
int i, count = lines.Count(); i < count; i++)
303 if (lines[i].EndsWith(
" {") && lines[i].StartsWith(GENERIC_WORLD_ENTITY_CLASS +
" "))
305 lines[i] = GENERIC_WORLD_ENTITY_CLASS +
" world : \"" + newWorldEntityPrefab +
"\" {";
313 PrintFormat(
"No %1 class can be found in %2", GENERIC_WORLD_ENTITY_CLASS, defaultLayerAbsFilePath, level:
LogLevel.WARNING);
317 bool result = SCR_FileIOHelper.WriteFileContent(defaultLayerAbsFilePath, lines);
319 Print(
"File written successfully: " + defaultLayerAbsFilePath,
LogLevel.NORMAL);
321 Print(
"File cannot be written: " + defaultLayerAbsFilePath,
LogLevel.WARNING);
330 protected static string GetDefaultLayerAbsoluteFilePath(
string relativeWorldFilePath)
332 string defaultLayerAbsFilePath;
333 if (!Workbench.GetAbsolutePath(relativeWorldFilePath, defaultLayerAbsFilePath))
335 Print(
"Cannot get absolute path for " + relativeWorldFilePath,
LogLevel.WARNING);
339 string layerAbsDir = FilePath.StripFileName(defaultLayerAbsFilePath);
340 if (!layerAbsDir.EndsWith(
"/"))
343 string worldName = FilePath.StripExtension(FilePath.StripPath(relativeWorldFilePath));
345 return layerAbsDir +
string.Format(LAYERS_DIR, worldName) +
"/" + DEFAULT_LAYER;
ResourceName resourceName
proto void Print(void var, LogLevel level=LogLevel.NORMAL)
Prints content of variable to console/log.
LogLevel
Enum with severity of the logging message.
proto void PrintFormat(string fmt, void param1=NULL, void param2=NULL, void param3=NULL, void param4=NULL, void param5=NULL, void param6=NULL, void param7=NULL, void param8=NULL, void param9=NULL, LogLevel level=LogLevel.NORMAL)
FileMode
Mode for opening file. See FileSystem::Open.
FileAttribute
File attributes. See FileDescription.