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

477 lines
20 KiB
C#

using TeaBox.Macros;
using System;
using System.IO;
using System.Linq;
using JD.Serialization;
using Path = JD.Serialization.Path;
namespace JDSongConverter.Tasks
{
/// <summary>
/// Converts the songdesc from the new (JD2016) format back to the old (JD2015) format
/// </summary>
#pragma warning disable 0618 //This warning is about obsolete classes but I need those to convert
public class ConvertTo2015SongDesc : Task
{
protected LuaSerializer m_luaSerializer;
protected XmlSerializer m_xmlSerializer;
protected String m_baseMapName = "";
protected String m_mainPath = "";
protected JD_SongDescTemplate m_songDesc2015;
protected JD_SongDescTemplate m_songDesc2016;
public override string Name
{
get { return "Convert to OLD JD2015 SongDesc Format"; }
set { }
}
public override string ProgressText
{
get { return "Converting to OLD JD2015 SongDesc Format"; }
}
protected String GetSongDir(String templatePath)
{
String songDir = "";
int index = templatePath.IndexOf("SongDesc.tpl");
if (index > -1)
{
songDir = templatePath.Substring(0, index);
}
return songDir;
}
public override TaskResult Process(IExecutionContext executionContext)
{
m_luaSerializer = new LuaSerializer();
m_luaSerializer.IgnoreDefaultValues = true;
m_xmlSerializer = new XmlSerializer();
m_baseMapName = Context.OutputMap.Name;
String baseNameLower = m_baseMapName.ToLower();
//If Partymaster or mashup version, it is handled when converting the main song, so skip
if (baseNameLower.EndsWith("pm") || (baseNameLower.EndsWith("mu") && !baseNameLower.EndsWith("cmu")))
return TaskResult.Ok;
String songDescTplPath = AppDefinitions.Default.AbsoluteMapPath(MapPathType.SongDescFile, Context.OutputMap.Folder, Context.Song.Name, Context.Song.Version, JDFolderLayout.Engine);
m_mainPath = GetSongDir(songDescTplPath);
var actorTemplate = m_luaSerializer.LoadObjectFromFile(songDescTplPath) as Actor_Template;
m_songDesc2016 = actorTemplate.COMPONENTS.OfType<JD_SongDescTemplate>().FirstOrDefault();
if (m_songDesc2016 == null)
return TaskResult.Error;
//re-create Template
convertBaseTemplate();
checkForSubVersions(checkMU: true);
checkForSubVersions(checkMU: false); //so Partymaster
//save template
actorTemplate.COMPONENTS.Clear();
actorTemplate.COMPONENTS.Add(m_songDesc2015);
m_luaSerializer.SaveObjectToFile(actorTemplate, songDescTplPath);
return TaskResult.Ok;
}
protected void convertBaseTemplate()
{
m_songDesc2015 = new JD_SongDescTemplate();
m_songDesc2015 = copyBaseInformation(m_songDesc2015, m_songDesc2016);
if (m_songDesc2016.AsyncPlayers.Count != 0)
{
m_songDesc2015.AsyncPlayers = m_songDesc2016.AsyncPlayers;
}
if (m_songDesc2016.Avatars.Count != 0)
{
m_songDesc2015.AsyncPlayers = m_songDesc2016.Avatars;
}
m_songDesc2015.DefaultColors = m_songDesc2016.DefaultColors;
//Create GameModeDesc Classic
JD_SongDescTemplate.GameModeDesc modeDescBase = new JD_SongDescTemplate.GameModeDesc();
modeDescBase = copyGameModeInformation(modeDescBase, m_songDesc2016);
//check tags
if (m_songDesc2016.Tags.Contains((StringID)"Alternate"))
modeDescBase.Flags = JD_SongDescTemplate.GameModeDesc.flags_Enum.SGM_SF_Classic_Alternate;
else if (m_songDesc2016.Tags.Contains((StringID)"StarRemix"))
modeDescBase.Flags = JD_SongDescTemplate.GameModeDesc.flags_Enum.SGM_SF_Classic_StarRemix;
else if (m_songDesc2016.Tags.Contains((StringID)"CommunityMashup"))
modeDescBase.Flags = JD_SongDescTemplate.GameModeDesc.flags_Enum.SGM_SF_Classic_CommunityMashup;
m_songDesc2015.GameModes.Add(modeDescBase);
}
protected void checkForSubVersions(bool checkMU /*if false, it checks for partymaster*/)
{
String subVersionMapName = m_baseMapName;
if (checkMU)
{
subVersionMapName = subVersionMapName + "MU";
}
else
{
subVersionMapName = subVersionMapName + "PM";
}
String filePath = System.IO.Path.Combine(m_mainPath, "SongDesc.tpl");
filePath = filePath.ReplaceIgnoreCase(m_baseMapName, subVersionMapName);
//check if PM and/or MU exist
if (File.Exists(filePath))
{
Actor_Template actorTemplate = m_luaSerializer.LoadObjectFromFile(filePath) as Actor_Template;
JD_SongDescTemplate subVersionSongDesc = actorTemplate.COMPONENTS.OfType<JD_SongDescTemplate>().FirstOrDefault();
if (subVersionSongDesc == null)
return;
//add to gamemodedesc
JD_SongDescTemplate.GameModeDesc modeDesc = new JD_SongDescTemplate.GameModeDesc();
modeDesc = copyGameModeInformation(modeDesc, subVersionSongDesc);
if (subVersionSongDesc.NumCoach != m_songDesc2016.NumCoach)
{
modeDesc.NumCoach = subVersionSongDesc.NumCoach;
}
if (subVersionSongDesc.DefaultColors.ContainsKey((StringID)"lyrics") && m_songDesc2016.DefaultColors.ContainsKey((StringID)"lyrics"))
{
if (subVersionSongDesc.DefaultColors[(StringID)"lyrics"] != m_songDesc2016.DefaultColors[(StringID)"lyrics"])
{
modeDesc.Colors = subVersionSongDesc.DefaultColors;
}
}
m_songDesc2015.GameModes.Add(modeDesc);
//copy the menuart
copyMenuArtInfo(subVersionMapName, !checkMU);
//copy the timeline stuff
copyTimelineInfo(subVersionMapName);
//Delete SubVersion Folder
String subversionFolder = m_mainPath.ToLower();
subversionFolder = subversionFolder.Replace(m_baseMapName.ToLower(), subVersionMapName);
Directory.Delete(subversionFolder,true);
}
}
protected JD_SongDescTemplate copyBaseInformation(JD_SongDescTemplate newSongDesc, JD_SongDescTemplate oldSongDesc)
{
newSongDesc.MapName = oldSongDesc.MapName;
newSongDesc.RelatedAlbums = oldSongDesc.RelatedAlbums;
newSongDesc.NumCoach = oldSongDesc.NumCoach;
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;
newSongDesc.AudioPreviewFadeTime = oldSongDesc.AudioPreviewFadeTime;
newSongDesc.AudioPreviews = oldSongDesc.AudioPreviews;
return newSongDesc;
}
protected JD_SongDescTemplate.GameModeDesc copyGameModeInformation(JD_SongDescTemplate.GameModeDesc _modeDesc, JD_SongDescTemplate _baseSongDesc)
{
if (_baseSongDesc.Mode == JD_SongDescTemplate.Mode_Enum.SongGameMode_Max)
{
if (_baseSongDesc.Tags.Contains((StringID)"Mashup"))
{
_modeDesc.Mode = JD_SongDescTemplate.Mode_Enum.SongGameMode_Mashup;
_baseSongDesc.Tags.Remove((StringID)"Mashup");
}
else if (_baseSongDesc.Tags.Contains((StringID)"Partymaster"))
{
_modeDesc.Mode = JD_SongDescTemplate.Mode_Enum.SongGameMode_PartyMaster;
_baseSongDesc.Tags.Remove((StringID)"Partymaster");
}
else
{
_modeDesc.Mode = JD_SongDescTemplate.Mode_Enum.SongGameMode_Classic;
}
}
_modeDesc.Status = _baseSongDesc.Status;
_modeDesc.LocaleID = _baseSongDesc.LocaleID.LocId;
_modeDesc.Mojo_value = _baseSongDesc.MojoValue;
_modeDesc.CountInProgression = _baseSongDesc.CountInProgression;
return _modeDesc;
}
#region Copy Timeline Info
protected void copyTimelineInfo(String _subVersionMapName)
{
String baseMapDir = System.IO.Path.Combine(m_mainPath, "Timeline", m_baseMapName + "_TML.isc");
String timelineDir = baseMapDir.ReplaceIgnoreCase(m_baseMapName, _subVersionMapName);
Path templatePath = Path.Empty;
Path actorPath = Path.Empty;
UAFList<Actor> actorList = new UAFList<Actor>();
Scene scene = m_xmlSerializer.LoadObjectFromFile(timelineDir) as Scene;
//Extracting the current subversion template and actor path from the scene
//and changing them to the new path within the scene
//then keeping all actors that are subversion specific (to be added to base timeline scene after)
for (int index = 0; index < scene.ACTORS.Count; ++index)
{
if (scene.ACTORS[index].USERFRIENDLY.Equals(_subVersionMapName, StringComparison.OrdinalIgnoreCase))
{
templatePath = scene.ACTORS[index].LUA;
actorPath = scene.ACTORS[index].INSTANCEDATAFILE;
String tempPath = templatePath.ToString().ReplaceIgnoreCase(_subVersionMapName + "/", m_baseMapName + "/");
scene.ACTORS[index].LUA = new Path(tempPath);
tempPath = actorPath.ToString().ReplaceIgnoreCase(_subVersionMapName + "/", m_baseMapName + "/");
scene.ACTORS[index].INSTANCEDATAFILE = new Path(tempPath);
actorList.Add(scene.ACTORS[index]);
}
}
addActorsToBaseMapScene(baseMapDir, actorList);
copySubVersionTimelineTemplate(templatePath, _subVersionMapName);
copyTimelineActor(actorPath, _subVersionMapName);
}
void copySubVersionTimelineTemplate(Path _templatePath, String _subVersionMapName)
{
if (_templatePath != Path.Empty)
{
String absolutePath = _templatePath.getAbsolute();
absolutePath = absolutePath.ToLower();
absolutePath = absolutePath.Replace(_subVersionMapName.ToLower() + "\\", m_baseMapName + "\\");
File.Copy(_templatePath.getAbsolute(), absolutePath, true);
String alltext = File.ReadAllText(absolutePath);
alltext = alltext.ReplaceIgnoreCase("\""+_subVersionMapName+"\"", "\""+m_baseMapName+"\"");
File.WriteAllText(absolutePath, alltext);
}
}
void copyTimelineActor(Path _actorPath, String _subVersionMapName)
{
if (_actorPath != Path.Empty)
{
var _actor = m_luaSerializer.LoadObjectFromFile(_actorPath.getAbsolute()) as Actor;
String tempLua = _actor.LUA.ToString();
tempLua = tempLua.ReplaceIgnoreCase(_subVersionMapName + "/", m_baseMapName + "/");
_actor.LUA = new Path(tempLua);
saveToFile(_actor, m_luaSerializer, _subVersionMapName, m_baseMapName, _actorPath.getAbsolute(), "\\");
}
}
#endregion
#region Copy MenuArt Info
protected void copyMenuArtInfo(String _subVersionMapName, bool _isPartyMaster)
{
String baseMenuArtDir = System.IO.Path.Combine(m_mainPath, "MenuArt", m_baseMapName + "_MENUART.isc");
String menuArtDir = baseMenuArtDir.ReplaceIgnoreCase(m_baseMapName, _subVersionMapName);
String gameMode = "Mashup";
if(_isPartyMaster)
gameMode = "PartyMaster";
//All three Lists are filled in the functions
UAFList<Actor> actorList = new UAFList<Actor>();
UAFList<Path> actorPathList = new UAFList<Path>();
UAFList<Path> actorNewPathList = new UAFList<Path>();
extractActorsFromSubMapMenuArtScene(menuArtDir, actorList, actorPathList, actorNewPathList, _subVersionMapName, gameMode);
addActorsToBaseMapScene(baseMenuArtDir, actorList);
copyMenuArtActors(actorPathList, actorNewPathList, _subVersionMapName, gameMode);
copyTextureFiles(actorPathList, actorNewPathList);
}
/// <summary>
/// ""world/jd2015/addictedtoyoumu/menuart/textures/addictedtoyoumu_cover_generic.tga",
/// to
/// "world/jd2015/addictedtoyou/menuart/textures/addictedtoyou_prelobby_mashup.tga",
/// OR
/// "world/jd2015/addictedtoyoumu/menuart/textures/addictedtoyoumu_coach_1.tga"
/// to
/// "world/jd2015/addictedtoyou/menuart/textures/addictedtoyou_mashup_coach_1.tga"
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
protected string ConvertMaterialPath(string name, string _subVersionMapName, string gameMode)
{
String tempPath = name.ReplaceIgnoreCase(_subVersionMapName, m_baseMapName);
tempPath = tempPath.ReplaceIgnoreCase("_coach_", "_" + gameMode + "_coach_");
tempPath = tempPath.ReplaceIgnoreCase("_cover_generic", "_Prelobby_" + gameMode);
return tempPath;
}
protected void extractActorsFromSubMapMenuArtScene(String _menuArtDir, UAFList<Actor> _actorList, UAFList<Path> _actorPathList, UAFList<Path> _newActorPathList, String _subVersionMapName, String gameMode)
{
Scene scene = m_xmlSerializer.LoadObjectFromFile(_menuArtDir) as Scene;
for (int index = 0; index < scene.ACTORS.Count; ++index)
{
String currentUserFriendly = scene.ACTORS[index].USERFRIENDLY;
if (currentUserFriendly.ContainsIgnoreCase("coach_") || currentUserFriendly.ContainsIgnoreCase("cover_generic"))
{
_actorPathList.Add(scene.ACTORS[index].INSTANCEDATAFILE);
if (currentUserFriendly.ContainsIgnoreCase("cover_generic"))
{
//In the old SongDesc userfriendlies files of gameMode covers were named like this, eg Birthday_Prelobby_Mashup
currentUserFriendly = m_baseMapName + "_Prelobby_" + gameMode;
}
else if (currentUserFriendly.ContainsIgnoreCase("_coach_"))
{
//In the old SongDesc userfriendlies files of gamemode coaches were named like this, eg Birthday_Mashup_Coach_1
currentUserFriendly = currentUserFriendly.ReplaceIgnoreCase(_subVersionMapName, m_baseMapName + "_" + gameMode);
}
scene.ACTORS[index].USERFRIENDLY = currentUserFriendly;
String tempPath = scene.ACTORS[index].INSTANCEDATAFILE.ToString();
tempPath = ConvertMaterialPath(tempPath, _subVersionMapName, gameMode);
scene.ACTORS[index].INSTANCEDATAFILE = new Path(tempPath);
_newActorPathList.Add(scene.ACTORS[index].INSTANCEDATAFILE);
_actorList.Add(scene.ACTORS[index]);
}
}
}
protected void copyMenuArtActors(UAFList<Path> _currentPathList, UAFList<Path> _newPathList, String _subVersionMapName, String _gameMode)
{
//Checking each menuArt actor if they contain copy-worthy texture paths
//Copying copy-worthy texture paths into the path arrays (the old and the new one)
//Saving the new actor in the new location
for (int index = 0; index < _currentPathList.Count; ++index)
{
var actor = m_luaSerializer.LoadObjectFromFile(_currentPathList[index].getAbsolute()) as Actor;
MaterialGraphicComponent comp = actor.COMPONENTS.OfType<MaterialGraphicComponent>().FirstOrDefault();
String diffusePath = comp.Material.TextureSet.Diffuse.ToString();
if (diffusePath.ContainsIgnoreCase(_subVersionMapName))
{
_currentPathList[index] = comp.Material.TextureSet.Diffuse;
diffusePath = ConvertMaterialPath(diffusePath, _subVersionMapName, _gameMode);
comp.Material.TextureSet.Diffuse = new Path(diffusePath);
String newPath = _newPathList[index].getAbsolute();
_newPathList[index] = comp.Material.TextureSet.Diffuse;
if (File.Exists(newPath) == false)
{
System.IO.Stream stream = File.Create(newPath);
stream.Close();
}
m_luaSerializer.SaveObjectToFile(actor, newPath);
}
else
{
//If it a generic texture (like cover_partymaster, which is in the _common folder, do not copy and remove the reference
//from the array
File.Copy(_currentPathList[index].getAbsolute(), _newPathList[index].getAbsolute(), true);
_currentPathList.RemoveAt(index);
_newPathList.RemoveAt(index);
--index;
}
}
}
protected void copyTextureFiles(UAFList<Path> _currentPathList, UAFList<Path> _newPathList)
{
for (int index = 0; index < _currentPathList.Count; ++index)
{
String oldDir = _currentPathList[index].getAbsolute();
String newDir = _newPathList[index].getAbsolute();
if (File.Exists(oldDir))
{
File.Copy(oldDir, newDir, true);
}
oldDir = oldDir.ReplaceIgnoreCase(".tga", ".atl");
newDir = newDir.ReplaceIgnoreCase(".tga", ".atl");
if (File.Exists(oldDir))
{
File.Copy(oldDir, newDir, true);
}
oldDir = oldDir.ReplaceIgnoreCase(".atl", ".tga.tfi");
newDir = newDir.ReplaceIgnoreCase(".atl", ".tga.tfi");
if (File.Exists(oldDir))
{
File.Copy(oldDir, newDir, true);
}
}
}
#endregion
#region Helpers
protected void cleanUpParentBind(String _newSongDir)
{
String alltext = File.ReadAllText(_newSongDir);
alltext = alltext.ReplaceIgnoreCase("<parentBind />", "");
File.WriteAllText(_newSongDir, alltext);
}
protected String saveToFile(object _objectToSave, Serializer _serializer, String _oldMapName, String _newMapName, String _oldSongDir, String _addOn = "")
{
String newSongDir = _oldSongDir.Replace(_oldMapName + _addOn, _newMapName + _addOn);
if (File.Exists(newSongDir) == false)
{
System.IO.Stream stream = File.Create(newSongDir);
stream.Close();
}
_serializer.SaveObjectToFile(_objectToSave, newSongDir);
return newSongDir;
}
protected void addActorsToBaseMapScene(String _baseDir, UAFList<Actor> _currentActorList)
{
Scene scene =m_xmlSerializer.LoadObjectFromFile(_baseDir) as Scene;
for (int index = 0; index < _currentActorList.Count; ++index)
{
scene.ACTORS.Add(_currentActorList[index]);
}
m_xmlSerializer.SaveObjectToFile(scene, _baseDir);
cleanUpParentBind(_baseDir);
}
#endregion
}
}