JD2022-TU1/main/tools/apps/JDSongConverter/Tasks/ConvertToNewSongDesc.cs

925 lines
No EOL
38 KiB
C#

using System.Linq;
using TeaBox.Macros;
using System.Collections.Generic;
using System;
using System.IO;
using System.Text.RegularExpressions;
using JD.Serialization;
using Path = JD.Serialization.Path;
namespace JDSongConverter.Tasks
{
/// <summary>
/// Converts the songdesc to the new (JD2016) format (without a GameModeDesc)
/// </summary>
#pragma warning disable 0618 //This warning is about obsolete classes but I need those to convert
public class ConvertToNewSongDesc : Task
{
protected LuaSerializer m_luaSerializer;
protected XmlSerializer m_xmlSerializer;
protected String8 m_mapNameString8;
protected String m_oldMapName = "";
protected String m_newMapName = "";
protected bool m_isPartyMaster = false;
public override string Name
{
get { return "Convert to JD2016 SongDesc Format"; }
set { }
}
public override string ProgressText
{
get { return "Converting to JD2016 SongDesc Format"; }
}
public override TaskResult Process(IExecutionContext executionContext)
{
String songDescTplPath = Context.OutputMap.SongDescPath;
String songDir = Context.OutputMap.Folder;
m_luaSerializer = new LuaSerializer();
m_luaSerializer.IgnoreDefaultValues = true;
m_xmlSerializer = new XmlSerializer();
Actor_Template actorTemplate = m_luaSerializer.LoadObject<Actor_Template>(songDescTplPath);
JD_SongDescTemplate oldSongDesc = actorTemplate.COMPONENTS.OfType<JD_SongDescTemplate>().FirstOrDefault();
if (oldSongDesc == null)
return TaskResult.Error;
if (oldSongDesc.IsNewFormat) //Already Converted
return TaskResult.Ok;
m_oldMapName = oldSongDesc.MapName;
m_mapNameString8 = new String8(m_oldMapName);
for (int index = 0; index < oldSongDesc.GameModes.Count; ++index)
{
JD_SongDescTemplate newSongDesc = new JD_SongDescTemplate();
String newSongDescTplPath = songDescTplPath;
newSongDesc = copyBaseInformation(newSongDesc, oldSongDesc);
JD_SongDescTemplate.GameModeDesc currentGameMode = oldSongDesc.GameModes[index];
if (currentGameMode.Mode == JD_SongDescTemplate.Mode_Enum.SongGameMode_Classic ||
currentGameMode.Mode == JD_SongDescTemplate.Mode_Enum.SongGameMode_Sweat)
{
newSongDesc.MapName = oldSongDesc.MapName;
newSongDesc.RelatedAlbums = oldSongDesc.RelatedAlbums;
newSongDesc.Mode = JD_SongDescTemplate.Mode_Enum.SongGameMode_Max;
newSongDesc.NumCoach = oldSongDesc.NumCoach;
newSongDesc.AudioPreviewFadeTime = oldSongDesc.AudioPreviewFadeTime;
newSongDesc.AudioPreviews = oldSongDesc.AudioPreviews;
switch (currentGameMode.Flags)
{
case JD_SongDescTemplate.GameModeDesc.flags_Enum.SGM_SF_Classic_Alternate:
newSongDesc.Tags.Add((StringID)"Alternate");
break;
case JD_SongDescTemplate.GameModeDesc.flags_Enum.SGM_SF_Classic_CommunityMashup:
newSongDesc.Tags.Add((StringID)"CommunityMashup");
break;
case JD_SongDescTemplate.GameModeDesc.flags_Enum.SGM_SF_Classic_StarRemix:
newSongDesc.Tags.Add((StringID)"CommunityMashup");
newSongDesc.Tags.Add((StringID)"StarRemix");
break;
}
if (oldSongDesc.AsyncPlayers.Count != 0)
{
newSongDesc.AsyncPlayers = oldSongDesc.AsyncPlayers;
}
if (currentGameMode.Mode == JD_SongDescTemplate.Mode_Enum.SongGameMode_Sweat)
{
newSongDesc.Tags.Add((StringID)"Sweat");
}
}
else
{
string ending = "MU";
m_isPartyMaster = false;
if (currentGameMode.Mode == JD_SongDescTemplate.Mode_Enum.SongGameMode_PartyMaster)
{
ending = "PM";
newSongDesc.Tags.Add((StringID)"Partymaster");
m_isPartyMaster = true;
}
else
{
newSongDesc.Tags.Add((StringID)"Mashup");
}
m_newMapName = m_oldMapName + ending;
newSongDesc.MapName = m_newMapName;
newSongDesc.RelatedAlbums = oldSongDesc.RelatedAlbums;
if (!newSongDesc.RelatedAlbums.Contains(m_mapNameString8))
{
newSongDesc.RelatedAlbums.Add(m_mapNameString8);
}
newSongDesc.Mode = JD_SongDescTemplate.Mode_Enum.SongGameMode_Max;
if (currentGameMode.NumCoach == JD_SongDescTemplate.NumCoach_Enum.SongCoachNum_Max)
{
newSongDesc.NumCoach = oldSongDesc.NumCoach;
}
else
{
newSongDesc.NumCoach = currentGameMode.NumCoach;
}
actorTemplate.TAGS.Clear();
actorTemplate.TAGS.Add(new String8("songdesc"));
//PM and MU need different treatment
newSongDescTplPath = songDescTplPath.Replace(oldSongDesc.MapName, newSongDesc.MapName);
if (File.Exists(newSongDescTplPath) == false)
{
System.IO.Stream stream = File.Create(newSongDescTplPath);
stream.Close();
}
//Copy all the stuff
copySongDescAct(songDir);
copyMainScene(songDir);
copyTimelineInfo(songDir);
copyMenuArtInfo(songDir);
copyRemainingScenes(songDir);
}
newSongDesc.Status = currentGameMode.Status;
newSongDesc.LocaleID = new LocalisationId(currentGameMode.LocaleID);
newSongDesc.MojoValue = currentGameMode.Mojo_value;
newSongDesc.CountInProgression = currentGameMode.CountInProgression;
if (currentGameMode.Colors.Count == 0)
{
newSongDesc.DefaultColors = oldSongDesc.DefaultColors;
}
else
{
newSongDesc.DefaultColors = currentGameMode.Colors;
}
if (oldSongDesc.Avatars.Count != 0)
{
newSongDesc.Avatars = oldSongDesc.Avatars;
}
actorTemplate.COMPONENTS.Clear();
actorTemplate.COMPONENTS.Add(newSongDesc);
m_luaSerializer.SaveObjectToFile(actorTemplate, newSongDescTplPath);
}
cleanUpOriginalTimeLine(songDir);
cleanUpOrginalMenuArtScene(songDir);
updateVideoLayer(songDir, m_oldMapName, false);
updateVideoLayer(songDir, m_oldMapName, true);
return TaskResult.Ok;
}
#region Copy Main Information
private JD_SongDescTemplate copyBaseInformation(JD_SongDescTemplate newSongDesc, JD_SongDescTemplate oldSongDesc)
{
newSongDesc.JDVersion = oldSongDesc.JDVersion;
newSongDesc.OriginalJDVersion = oldSongDesc.OriginalJDVersion;
newSongDesc.Artist = oldSongDesc.Artist;
newSongDesc.DancerName = oldSongDesc.DancerName;
newSongDesc.Title = oldSongDesc.Title;
newSongDesc.Difficulty = oldSongDesc.Difficulty;
newSongDesc.LyricsType = oldSongDesc.LyricsType;
newSongDesc.BackgroundType = oldSongDesc.BackgroundType;
newSongDesc.MainCoach = oldSongDesc.MainCoach;
return newSongDesc;
}
private void copySongDescAct(String _oldSongDir)
{
_oldSongDir = System.IO.Path.Combine(_oldSongDir, "SongDesc.act");
var _actor = (Actor)m_luaSerializer.LoadObjectFromFile(_oldSongDir);
if (_actor == null)
return;
String tempLua = _actor.LUA.ToString();
tempLua = tempLua.ReplaceIgnoreCase(m_oldMapName, m_newMapName); // replacing mapname while ignoring casing
_actor.LUA = new Path(tempLua);
saveToFile(_actor, m_luaSerializer, _oldSongDir);
}
private void copyMainScene(String _oldSongDir)
{
String fileEnding = String.Concat(m_oldMapName, "_MAIN_SCENE.isc");
_oldSongDir = System.IO.Path.Combine(_oldSongDir, fileEnding);
Scene scene = m_xmlSerializer.LoadObject<Scene>(_oldSongDir);
for (int index = 0; index < scene.ACTORS.Count; ++index)
{
String currentUserFriendly = scene.ACTORS[index].USERFRIENDLY;
String tempPath;
if (currentUserFriendly.Contains(": "))
{
int indexColon = currentUserFriendly.IndexOf(":");
if (indexColon != -1)
{
currentUserFriendly = currentUserFriendly.Replace(currentUserFriendly.Substring(0, indexColon), m_newMapName + " :");
scene.ACTORS[index].USERFRIENDLY = currentUserFriendly;
tempPath = scene.ACTORS[index].INSTANCEDATAFILE.ToString();
tempPath = tempPath.ReplaceIgnoreCase(m_oldMapName, m_newMapName);
scene.ACTORS[index].INSTANCEDATAFILE = new Path(tempPath);
tempPath = scene.ACTORS[index].LUA.ToString();
tempPath = tempPath.ReplaceIgnoreCase(m_oldMapName, m_newMapName);
scene.ACTORS[index].LUA = new Path(tempPath);
}
}
else
{
SubSceneActor currentSubScene = (SubSceneActor)scene.ACTORS[index];
tempPath = currentSubScene.RELATIVEPATH.ToString();
tempPath = tempPath.ReplaceIgnoreCase(m_oldMapName, m_newMapName);
currentSubScene.RELATIVEPATH = new Path(tempPath);
currentUserFriendly = currentUserFriendly.ReplaceIgnoreCase(m_oldMapName, m_newMapName);
scene.ACTORS[index].USERFRIENDLY = currentUserFriendly;
}
}
for (int index = 0; index < scene.PLATFORM_FILTER.Count; ++index)
{
TargetFilterList targetfilter = scene.PLATFORM_FILTER[index];
for (int counter = 0; counter < targetfilter.Objects.Count; ++counter)
{
String temp = targetfilter.Objects[index].ToString();
if (temp.ContainsIgnoreCase(m_oldMapName))
{
temp = temp.ReplaceIgnoreCase(m_oldMapName, m_newMapName);
targetfilter.Objects[index] = new String8(temp);
}
}
}
String newDir = saveToFile(scene, m_xmlSerializer, _oldSongDir);
cleanUpParentBind(newDir);
}
#endregion
#region Copy Timeline Info
private void copyTimelineInfo(String _oldSongDir)
{
String oldTimelineDir = System.IO.Path.Combine(_oldSongDir, "Timeline");
copyTimelineScene(oldTimelineDir);
copyTimelineActor(oldTimelineDir);
copyTimelineTemplate(oldTimelineDir);
}
private void copyTimelineScene(String _oldTimelineDir)
{
String fileEnding = String.Concat(m_oldMapName, "_TML.isc");
_oldTimelineDir = System.IO.Path.Combine(_oldTimelineDir, fileEnding);
Scene scene = m_xmlSerializer.LoadObject<Scene>(_oldTimelineDir);
for (int index = 0; index < scene.ACTORS.Count; ++index)
{
String currentUserFriendly = scene.ACTORS[index].USERFRIENDLY;
String tempPath;
if (!currentUserFriendly.ContainsIgnoreCase("_TML_"))
{
if (currentUserFriendly.Equals(m_newMapName, StringComparison.OrdinalIgnoreCase))
{
tempPath = scene.ACTORS[index].INSTANCEDATAFILE.ToString();
tempPath = tempPath.ReplaceIgnoreCase(m_oldMapName + "/", m_newMapName + "/");
scene.ACTORS[index].INSTANCEDATAFILE = new Path(tempPath);
tempPath = scene.ACTORS[index].LUA.ToString();
tempPath = tempPath.ReplaceIgnoreCase(m_oldMapName + "/", m_newMapName + "/");
scene.ACTORS[index].LUA = new Path(tempPath);
}
else
{
scene.ACTORS.RemoveAt(index);
index--;
}
}
}
String newDir = saveToFile(scene, m_xmlSerializer, _oldTimelineDir);
cleanUpParentBind(newDir);
}
private void copyTimelineTemplate(String _oldTimelineDir)
{
String fileEnding = String.Concat(m_newMapName, ".tpl");
_oldTimelineDir = System.IO.Path.Combine(_oldTimelineDir, fileEnding);
String newSongDir = _oldTimelineDir.Replace(m_oldMapName+"\\", m_newMapName+"\\");
File.Copy(_oldTimelineDir, newSongDir, true);
File.Delete(_oldTimelineDir);
String alltext = File.ReadAllText(newSongDir);
alltext = alltext.ReplaceIgnoreCase("\""+m_oldMapName+"\"", "\""+m_newMapName+"\"");
File.WriteAllText(newSongDir, alltext);
}
private void copyTimelineActor(String _oldTimelineDir)
{
String fileEnding = String.Concat(m_newMapName, ".act");
_oldTimelineDir = System.IO.Path.Combine(_oldTimelineDir, fileEnding);
var _actor = m_luaSerializer.LoadObject<Actor>(_oldTimelineDir);
String tempLua = _actor.LUA.ToString();
tempLua = tempLua.ReplaceIgnoreCase(m_oldMapName + "/", m_newMapName + "/");
_actor.LUA = new Path(tempLua);
saveToFile(_actor, m_luaSerializer, _oldTimelineDir, "\\");
File.Delete(_oldTimelineDir);
}
#endregion
#region Clean Up Original Timeline
private void cleanUpOriginalTimeLine(String _oldSongDir)
{
String oldTimelineDir = System.IO.Path.Combine(_oldSongDir, "Timeline");
String fileEnding = String.Concat(m_oldMapName, "_TML.isc");
oldTimelineDir = System.IO.Path.Combine(oldTimelineDir, fileEnding);
Scene scene = m_xmlSerializer.LoadObject<Scene>(oldTimelineDir);
for (int index = 0; index < scene.ACTORS.Count; ++index)
{
String currentUserFriendly = scene.ACTORS[index].USERFRIENDLY;
if (!currentUserFriendly.ContainsIgnoreCase("_TML_"))
{
scene.ACTORS.RemoveAt(index);
index--;
}
}
m_xmlSerializer.SaveObjectToFile(scene, oldTimelineDir);
cleanUpParentBind(oldTimelineDir);
}
#endregion
#region Copy MenuArt Info
private void copyMenuArtInfo(String _oldSongDir)
{
String oldTimelineDir = System.IO.Path.Combine(_oldSongDir, "MenuArt");
copyMenuArtScene(oldTimelineDir);
copyMenuArtActors(oldTimelineDir);
copyMenuArtTextures(oldTimelineDir);
}
private List<Path> m_actorPathList = new List<Path>();
private int m_coachMUcounter = 0;
private int m_coachCounter = 0;
private const String MASHUP_STRING = "mashup";
private const String PARTYMASTER_STRING = "partymaster";
private void copyMenuArtScene(String _oldMenuArtDir)
{
m_actorPathList.Clear();
m_coachMUcounter = 0;
m_coachCounter = 0;
String fileEnding = String.Concat(m_oldMapName, "_MENUART.isc");
_oldMenuArtDir = System.IO.Path.Combine(_oldMenuArtDir, fileEnding);
Scene scene = m_xmlSerializer.LoadObject<Scene>(_oldMenuArtDir);
for (int index = 0; index < scene.ACTORS.Count; ++index)
{
String currentUserFriendly = scene.ACTORS[index].USERFRIENDLY;
if ((m_isPartyMaster && currentUserFriendly.ContainsIgnoreCase(PARTYMASTER_STRING)) ||
(!m_isPartyMaster && currentUserFriendly.ContainsIgnoreCase(MASHUP_STRING)))
{
if (!m_isPartyMaster && (currentUserFriendly.ContainsIgnoreCase("coach")))
{
m_coachMUcounter++;
}
m_actorPathList.Add(scene.ACTORS[index].INSTANCEDATAFILE);
scene.ACTORS.RemoveAt(index);
index--;
continue;
}
else if ((m_isPartyMaster && currentUserFriendly.ContainsIgnoreCase(MASHUP_STRING)) ||
(!m_isPartyMaster && currentUserFriendly.ContainsIgnoreCase(PARTYMASTER_STRING)))
{
scene.ACTORS.RemoveAt(index);
index--;
continue;
}
if (currentUserFriendly.ContainsIgnoreCase("_coach"))
{
m_coachCounter++;
}
currentUserFriendly = scene.ACTORS[index].USERFRIENDLY;
currentUserFriendly = currentUserFriendly.ReplaceIgnoreCase(m_oldMapName + "_", m_newMapName + "_");
scene.ACTORS[index].USERFRIENDLY = currentUserFriendly;
String tempPath = scene.ACTORS[index].INSTANCEDATAFILE.ToString();
tempPath = tempPath.ReplaceIgnoreCase(m_oldMapName + "/", m_newMapName + "/");
tempPath = tempPath.ReplaceIgnoreCase(m_oldMapName+"_", m_newMapName+"_");
scene.ACTORS[index].INSTANCEDATAFILE = new Path(tempPath);
scene.ACTORS[index].COMPONENTS.Clear();
}
if (!m_isPartyMaster && m_coachMUcounter != m_coachCounter)
{
for (int index = 0; index < scene.ACTORS.Count; ++index)
{
String currentUserFriendly = scene.ACTORS[index].USERFRIENDLY;
if (currentUserFriendly.ContainsIgnoreCase("coach_"))
{
for (int inner = m_coachMUcounter + 1; inner <= m_coachCounter; ++inner)
{
if (currentUserFriendly.ContainsIgnoreCase("coach_" + inner))
{
scene.ACTORS.RemoveAt(index);
index--;
break;
}
}
}
}
}
String newDir = saveToFile(scene, m_xmlSerializer, _oldMenuArtDir);
cleanUpParentBind(newDir);
}
private List<Path> m_texturePaths = new List<Path>();
private void copyMenuArtActors(String _oldMenuArtDir)
{
m_texturePaths.Clear();
String tempPath = "";
bool needsGeneric = true;
//Album Coach
String fileEnding = String.Concat(m_oldMapName, "_Cover_AlbumCoach.act");
String filePath = System.IO.Path.Combine(_oldMenuArtDir, "Actors", fileEnding);
updateMaterialGraphicComponent(filePath);
//Album Background
fileEnding = String.Concat(m_oldMapName, "_Cover_AlbumBkg.act");
filePath = System.IO.Path.Combine(_oldMenuArtDir, "Actors", fileEnding);
updateMaterialGraphicComponent(filePath);
//if has prelobby or coach, copy that otherwise generic
for (int index = 0; index < m_actorPathList.Count; ++index)
{
tempPath = m_actorPathList[index].getAbsolute();
if (tempPath.ContainsIgnoreCase("prelobby"))
{
needsGeneric = false;
}
updateMaterialGraphicComponent(tempPath);
File.Delete(tempPath);
}
if (needsGeneric)
{
fileEnding = String.Concat(m_oldMapName, "_Cover_Generic.act");
filePath = System.IO.Path.Combine(_oldMenuArtDir, "Actors", fileEnding);
updateMaterialGraphicComponent(filePath);
}
//If there was not a special number of coaches, copy original ones
if (m_coachMUcounter == 0)
{
for (int index = 1; index <= m_coachCounter; ++index )
{
fileEnding = String.Concat(m_oldMapName, "_Coach_" + index +".act");
filePath = System.IO.Path.Combine(_oldMenuArtDir, "Actors", fileEnding);
updateMaterialGraphicComponent(filePath);
}
}
}
private void copyMenuArtTextures(String _oldMenuArtDir)
{
String tempPath = "";
for (int index = 0; index < m_texturePaths.Count; ++index)
{
tempPath = m_texturePaths[index].getAbsolute();
if (!tempPath.ContainsIgnoreCase("\\cover_partymaster."))
{
String newPath = ConvertMaterialName(tempPath);
if (!newPath.ContainsIgnoreCase("_cover") &&
!newPath.ContainsIgnoreCase("_coach_") &&
newPath.ContainsIgnoreCase(m_newMapName))
{
newPath = newPath.Replace(m_newMapName + ".", m_newMapName + "_cover_generic.");
}
//.TGA
if (File.Exists(tempPath))
{
File.Copy(tempPath, newPath, true);
}
tempPath = tempPath.Replace(".tga", ".atl");
newPath = newPath.Replace(".tga", ".atl");
//ATL
if (File.Exists(tempPath))
{
File.Copy(tempPath, newPath, true);
}
tempPath = tempPath.Replace(".atl", ".tga.tfi");
newPath = newPath.Replace(".atl", ".tga.tfi");
if (File.Exists(tempPath))
{
File.Copy(tempPath, newPath, true);
}
//Deleting all the specific ones
tempPath = m_texturePaths[index].getAbsolute();
if (tempPath.ContainsIgnoreCase(MASHUP_STRING) ||
tempPath.ContainsIgnoreCase(PARTYMASTER_STRING))
{
File.Delete(tempPath);
tempPath = tempPath.Replace(".tga", ".atl");
File.Delete(tempPath);
tempPath = tempPath.Replace(".atl", ".tga.tfi");
File.Delete(tempPath);
}
}
}
}
private void updateMaterialGraphicComponent(String _filePath)
{
String tempPath = "";
Actor actor = (Actor)m_luaSerializer.LoadObject<Actor>(_filePath);
MaterialGraphicComponent comp = actor.COMPONENTS.OfType<MaterialGraphicComponent>().FirstOrDefault();
m_texturePaths.Add(comp.Material.TextureSet.Diffuse);
tempPath = comp.Material.TextureSet.Diffuse.ToString();
tempPath = ConvertMaterialName(tempPath);
if (!tempPath.ContainsIgnoreCase("_cover") &&
!tempPath.ContainsIgnoreCase("_coach_") &&
tempPath.ContainsIgnoreCase(m_newMapName))
{
tempPath = tempPath.Replace(m_newMapName + ".", m_newMapName + "_cover_generic.");
}
comp.Material.TextureSet.Diffuse = new Path(tempPath);
_filePath = ConvertMaterialName(_filePath);
_filePath = _filePath.ReplaceIgnoreCase(m_newMapName, m_oldMapName);
saveToFile(actor, m_luaSerializer, _filePath);
}
/// <summary>
/// "world/jd2015/addictedtoyou/menuart/textures/addictedtoyou_prelobby_mashup.tga",
/// to
/// "world/jd2015/addictedtoyoumu/menuart/textures/addictedtoyoumu_cover_generic.tga",
/// OR
/// world/jd2015/addictedtoyou/menuart/textures/addictedtoyou_mashup_coach_1.tga
/// to
/// "world/jd2015/addictedtoyoumu/menuart/textures/addictedtoyoumu_coach_1.tga"
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
private string ConvertMaterialName(string name)
{
string tempPath = name.ReplaceIgnoreCase(m_oldMapName + "_" + PARTYMASTER_STRING, "");
tempPath = tempPath.ReplaceIgnoreCase(m_oldMapName, m_newMapName);
tempPath = tempPath.ReplaceIgnoreCase("_" + MASHUP_STRING, "");
tempPath = tempPath.ReplaceIgnoreCase("_prelobby", "_cover_generic");
return tempPath;
}
#endregion
#region Clean Up Original MenuArt
private void cleanUpOrginalMenuArtScene(String _oldSongDir)
{
String fileEnding = String.Concat(m_oldMapName, "_MENUART.isc");
_oldSongDir = System.IO.Path.Combine(_oldSongDir, "MenuArt", fileEnding);
Scene scene = m_xmlSerializer.LoadObject<Scene>(_oldSongDir);
for (int index = 0; index < scene.ACTORS.Count; ++index)
{
String currentUserFriendly = scene.ACTORS[index].USERFRIENDLY;
if (currentUserFriendly.ContainsIgnoreCase(PARTYMASTER_STRING) ||
currentUserFriendly.ContainsIgnoreCase(MASHUP_STRING))
{
scene.ACTORS.RemoveAt(index);
index--;
continue;
}
}
m_xmlSerializer.SaveObjectToFile(scene, _oldSongDir);
cleanUpParentBind(_oldSongDir);
}
#endregion
#region Copy Remaining Scenes
private void copyRemainingScenes(String _oldSongDir)
{
copyAudioLayer(_oldSongDir);
copyAutodanceLayer(_oldSongDir);
copyGraphLayer(_oldSongDir);
copyCinematicsLayer(_oldSongDir);
copyVideoLayer(_oldSongDir);
}
private void copyAudioLayer(String _oldSongDir)
{
simpleCopyScene(_oldSongDir, "_AUDIO.isc", "Audio");
}
private void copyCinematicsLayer(String _oldSongDir)
{
String fileEnding = String.Concat(m_oldMapName, "_CINE.isc");
String oldLayerDir = System.IO.Path.Combine(_oldSongDir, "Cinematics");
String filePath = System.IO.Path.Combine(oldLayerDir, fileEnding);
copySceneExchangeSongName(filePath);
fileEnding = String.Concat(m_oldMapName, "_MainSequence.act");
filePath = System.IO.Path.Combine(oldLayerDir, fileEnding);
copyActorExchangeSongName(filePath);
fileEnding = String.Concat(m_oldMapName, "_MainSequence.tpl");
filePath = System.IO.Path.Combine(oldLayerDir, fileEnding);
Actor_Template actorTemplate = m_luaSerializer.LoadObject<Actor_Template>(filePath);
String tempPath;
MasterTape_Template masterTape = actorTemplate.COMPONENTS.OfType<MasterTape_Template>().FirstOrDefault();
foreach (TapeGroup group in masterTape.TapesRack)
{
for (int index = 0; index < group.Entries.Count; ++index )
{
tempPath = group.Entries[index].Path.ToString();
tempPath = tempPath.ReplaceIgnoreCase(m_oldMapName, m_newMapName);
group.Entries[index].Path = new Path(tempPath);
}
}
saveToFile(actorTemplate, m_luaSerializer, filePath);
//add tape
Tape tape = new Tape();
fileEnding = String.Concat(m_oldMapName, "_MainSequence.tape");
filePath = System.IO.Path.Combine(oldLayerDir, fileEnding);
saveToFile(tape, m_luaSerializer, filePath);
}
private void copyGraphLayer(String _oldSongDir)
{
simpleCopyScene(_oldSongDir, "_GRAPH.isc", "Graph");
}
private void copyVideoLayer(String _oldSongDir)
{
String fileEnding = String.Concat(m_oldMapName, "_VIDEO.isc");
String oldLayerDir = System.IO.Path.Combine(_oldSongDir, "VideosCoach");
String filePath = System.IO.Path.Combine(oldLayerDir, fileEnding);
simpleCopyScene(_oldSongDir, "_VIDEO.isc", "VideosCoach");
simpleCopyScene(_oldSongDir, "_VIDEO_alpha.isc", "VideosCoach");
String newSongDir = _oldSongDir.ToLower();
newSongDir = newSongDir.Replace(m_oldMapName.ToLower(), m_newMapName);
updateVideoLayer(newSongDir, m_newMapName, false);
updateVideoLayer(newSongDir, m_newMapName, true);
}
private void copyAutodanceLayer(String _oldSongDir)
{
String fileEnding = String.Concat(m_oldMapName, "_AUTODANCE.isc");
String oldLayerDir = System.IO.Path.Combine(_oldSongDir, "Autodance");
String filePath = System.IO.Path.Combine(oldLayerDir, fileEnding);
copySceneExchangeSongName(filePath);
fileEnding = String.Concat(m_oldMapName, "_Autodance.act");
filePath = System.IO.Path.Combine(oldLayerDir, fileEnding);
copyActorExchangeSongName(filePath);
fileEnding = String.Concat(m_oldMapName, "_Autodance.tpl");
filePath = System.IO.Path.Combine(oldLayerDir, fileEnding);
Actor_Template actorTemplate = (Actor_Template)m_luaSerializer.LoadObjectFromFile(filePath);
if (actorTemplate == null)
return;
JD_AutodanceComponent_Template autoDance = actorTemplate.COMPONENTS.OfType<JD_AutodanceComponent_Template>().FirstOrDefault();
autoDance.Song = m_newMapName;
saveToFile(actorTemplate, m_luaSerializer, filePath);
fileEnding = String.Concat(m_oldMapName, "_prop0.act");
filePath = System.IO.Path.Combine(_oldSongDir, "Autodance", "Props", fileEnding);
String newFilePath = filePath.ReplaceIgnoreCase(m_oldMapName, m_newMapName);
if (File.Exists(filePath))
{
File.Copy(filePath, newFilePath, true);
}
}
#endregion
#region Helpers
private String saveToFile(object _objectToSave, Serializer _serializer, String _oldSongDir, String _addOn = "")
{
String newSongDir = _oldSongDir.Replace(m_oldMapName + _addOn, m_newMapName + _addOn);
if (File.Exists(newSongDir) == false)
{
System.IO.Stream stream = File.Create(newSongDir);
stream.Close();
}
_serializer.SaveObjectToFile(_objectToSave, newSongDir);
return newSongDir;
}
private void cleanUpParentBind(String _newSongDir)
{
String alltext = File.ReadAllText(_newSongDir);
alltext = alltext.ReplaceIgnoreCase("<parentBind />", "");
File.WriteAllText(_newSongDir, alltext);
}
private void simpleCopyScene(String _oldSongDir, String _sceneName, String _subDir)
{
String fileEnding = String.Concat(m_oldMapName, _sceneName);
String oldGraphDir = System.IO.Path.Combine(_oldSongDir, _subDir, fileEnding);
String newSongDir = oldGraphDir.ReplaceIgnoreCase(m_oldMapName, m_newMapName);
if (File.Exists(oldGraphDir))
{
File.Copy(oldGraphDir, newSongDir, true);
}
}
private void copySceneExchangeSongName(String _filePath)
{
Scene scene = m_xmlSerializer.LoadObject<Scene>(_filePath);
String tempPath;
for (int index = 0; index < scene.ACTORS.Count; ++index)
{
String currentUserFriendly = scene.ACTORS[index].USERFRIENDLY;
currentUserFriendly = currentUserFriendly.ReplaceIgnoreCase(m_oldMapName, m_newMapName);
scene.ACTORS[index].USERFRIENDLY = currentUserFriendly;
tempPath = scene.ACTORS[index].INSTANCEDATAFILE.ToString().ReplaceIgnoreCase(m_oldMapName, m_newMapName);
scene.ACTORS[index].INSTANCEDATAFILE = new Path(tempPath);
tempPath = scene.ACTORS[index].LUA.ToString().ReplaceIgnoreCase( m_oldMapName, m_newMapName);
scene.ACTORS[index].LUA = new Path(tempPath);
}
String newDir = saveToFile(scene, m_xmlSerializer, _filePath);
cleanUpParentBind(newDir);
}
private void copyActorExchangeSongName(String _fileName)
{
Actor actor = m_luaSerializer.LoadObject<Actor>(_fileName);
String tempPath = actor.LUA.ToString().ReplaceIgnoreCase(m_oldMapName, m_newMapName);
actor.LUA = new Path(tempPath);
saveToFile(actor, m_luaSerializer, _fileName);
}
private void updateVideoLayer(String _songDir, String _songName, bool isAlpha)
{
String fileEnding = "_VIDEO";
String pathActor = "";
Path pathTemplate = Path.Empty;
PleoComponent pleoComp = null;
if (isAlpha)
{
fileEnding = string.Concat(_songName, fileEnding, "_alpha.isc");
}
else
{
fileEnding = string.Concat(_songName, fileEnding, ".isc");
}
String filePath = System.IO.Path.Combine(_songDir, "VideosCoach", fileEnding);
if (File.Exists(filePath))
{
Scene scene = m_xmlSerializer.LoadObject<Scene>(filePath);
for (int index = 0; index < scene.ACTORS.Count; ++index)
{
if (scene.ACTORS[index].USERFRIENDLY.Equals("VideoScreen", StringComparison.OrdinalIgnoreCase))
{
pathTemplate = scene.ACTORS[index].LUA;
pathActor = scene.ACTORS[index].INSTANCEDATAFILE.ToString();
pathActor = pathActor.ReplaceIgnoreCase("_common", _songName);
pathActor = pathActor.ReplaceIgnoreCase(m_oldMapName + "/", _songName + "/");
pathActor = pathActor.ReplaceIgnoreCase("videoscreen", "Videoscoach");
if (isAlpha && pathActor.ContainsIgnoreCase("_player_main.act"))
{
pathActor = pathActor.ToLower();
pathActor = pathActor.Replace("_player_main.act", "_player_alpha.act");
}
scene.ACTORS[index].INSTANCEDATAFILE = new Path(pathActor);
pathActor = scene.ACTORS[index].INSTANCEDATAFILE.getAbsolute();
pleoComp = scene.ACTORS[index].COMPONENTS.OfType<PleoComponent>().FirstOrDefault();
scene.ACTORS[index].COMPONENTS.Remove(pleoComp);
}
}
m_xmlSerializer.SaveObjectToFile(scene, filePath);
cleanUpParentBind(filePath);
if (pleoComp != null && pleoComp.Video.ToString() != "")
{
Actor actor = new Actor();
actor.LUA = pathTemplate;
actor.COMPONENTS.Add(pleoComp);
if (File.Exists(pathActor) == false)
{
System.IO.Stream stream = File.Create(pathActor);
stream.Close();
}
m_luaSerializer.SaveObjectToFile(actor, pathActor);
}
else
{
String orgFile = pathActor.ToLower();
orgFile = orgFile.Replace(m_newMapName.ToLower(), m_oldMapName);
if (orgFile.Equals(pathActor, StringComparison.OrdinalIgnoreCase) == false)
{
if (File.Exists(orgFile))
{
File.Copy(orgFile, pathActor, true);
}
}
}
}
}
#endregion
}
}
public static class ConversionExtensions
{
public static bool ContainsIgnoreCase(this string value, string expression)
{
return value.ToLower().Contains(expression.ToLower()) ;
}
// replacing while ignoring casing
public static string ReplaceIgnoreCase(this string value, string expression, string replacement)
{
return Regex.Replace(value,"(?i)"+expression, replacement);
}
public static T LoadObject<T>(this Serializer serializer, string fileName)
{
T result = (T)serializer.LoadObjectFromFile(fileName);
if (result == null)
throw new Exception("Cannot read: "+ fileName);
return result;
}
}