Source code for the scripts converter.

NOTE: It is pretty straighforward and ugly, so make sure you check all the changes it does. :P

--HG--
branch : trunk
This commit is contained in:
azazel
2010-08-09 00:01:35 +06:00
parent 1f80c7b3c0
commit 39ab39f6cb
5 changed files with 347 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScriptConverter", "ScriptConverter\ScriptConverter.csproj", "{AB4C10BF-53AD-44AC-8B7A-B3F406DF468A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AB4C10BF-53AD-44AC-8B7A-B3F406DF468A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB4C10BF-53AD-44AC-8B7A-B3F406DF468A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB4C10BF-53AD-44AC-8B7A-B3F406DF468A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AB4C10BF-53AD-44AC-8B7A-B3F406DF468A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,226 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
namespace ScriptConverter {
class Program {
static void Main(string[] args) {
if (args.Length != 1) {
Console.WriteLine("Usage: ScriptsConverter.exe [path_to_dir|path_to_file]");
} else {
string path = args[0];
if (File.Exists(path)) {
ProcessFile(path);
} else if (Directory.Exists(path)) {
ProcessDirectory(path);
} else {
Console.WriteLine("Invalid file or directory specified.\r\n\r\nUsage: ScriptsConverter.exe [path_to_dir|path_to_file]");
}
}
}
static void ProcessDirectory(string path) {
string[] files = Directory.GetFiles(path, "*.cpp");
foreach (string file in files) {
ProcessFile(file);
}
string[] dirs = Directory.GetDirectories(path);
foreach (string dir in dirs) {
ProcessDirectory(dir);
}
}
class ScriptData {
public int type = 0;
public string name;
public ArrayList methods = new ArrayList();
public string instanceName = null;
public string aiName = null;
public string[] special = new string[] { "GetAI_", "GetInstance_", "GetInstanceData_" };
public void AddMethod(string method) {
methods.Add(method);
int i = 0;
foreach (string s in special) {
++i;
int pos = method.IndexOf(s);
if (pos != -1) {
type = i;
string name = method.Substring(pos + s.Length);
if (i == 1) {
aiName = name + "AI";
}
if (i == 2 || i == 3)
instanceName = name;
}
}
}
public override string ToString() {
StringBuilder sb = new StringBuilder();
sb.AppendFormat("Script: {0}\n", name);
foreach (string method in methods)
sb.Append(" ").Append(method).Append("\n");
sb.Append("\n");
return sb.ToString();
}
}
static string GetMethod(string method, ref string txt, ref int minPos) {
string res = null;
Regex r = new Regex(method + "(\\s|:|[(])");
Match m = r.Match(txt);
if (m.Success) {
int pos = m.Index;
while (pos-- >= 0 && pos < txt.Length) {
if (txt[pos] == '\n') break;
}
//pos++;
int lastPos = txt.IndexOf("\n}", pos);
if (lastPos != -1) {
lastPos += 2;
while (lastPos++ >= 0 && lastPos < txt.Length) {
if (txt[lastPos] == '\n') break;
}
res = txt.Substring(pos, lastPos - pos);
txt = txt.Remove(pos, lastPos - pos);
}
if (pos < minPos)
minPos = pos;
}
return res;
}
static void ProcessFile(string filePath) {
Console.WriteLine(filePath);
string txt = File.ReadAllText(filePath);
string[] lines = File.ReadAllLines(filePath);
Array.Reverse(lines);
ArrayList scripts = new ArrayList();
ScriptData data = null;
bool scriptStart = false;
foreach (string line in lines) {
if (line.IndexOf("Script *") != -1) {
break;
}
if (line.IndexOf("->RegisterSelf();") != -1) {
scriptStart = true;
data = new ScriptData();
continue;
}
if (scriptStart) {
if (line.IndexOf("= new Script") != -1) {
scriptStart = false;
scripts.Add(data);
data = null;
continue;
}
Regex r = new Regex("newscript->([a-zA-Z]+) *= *&?([\"_a-zA-Z0-9]+);");
Match m = r.Match(line);
if (m.Success) {
if (m.Groups[1].Value.Equals("Name")) {
data.name = m.Groups[2].Value.Trim(new char[] { '"' });
} else {
data.AddMethod(m.Groups[2].Value);
}
}
continue;
}
}
if (scripts.Count != 0) {
string register = "";
foreach (ScriptData sd in scripts) {
string ss = "";
Console.WriteLine(sd);
int minPos = txt.Length;
foreach (string method in sd.methods) {
string s = GetMethod(method, ref txt, ref minPos);
ss += s + "\n";
}
if (sd.instanceName != null) {
string s = GetMethod("struct " + sd.instanceName, ref txt, ref minPos);
ss += s + "\n";
}
if (sd.aiName != null) {
string ai = GetMethod("struct " + sd.aiName, ref txt, ref minPos);
if (ai != null) {
string sm = null;
Regex r = new Regex("\\S+ " + sd.aiName + "::([^( ]+)");
while (r.IsMatch(txt)) {
Match m = r.Match(txt);
int startPos = m.Index;
int endPos = txt.IndexOf("\n}", startPos);
if (endPos != -1)
endPos += 2;
while (endPos++ >= 0 && endPos < txt.Length) {
if (txt[endPos] == '\n') break;
}
sm = txt.Substring(startPos, endPos - startPos);
txt = txt.Remove(startPos, endPos - startPos);
if (sm != null) {
sm = sm.Replace("\n", "\n ");
Regex r1 = new Regex("\\S+ " + m.Groups[1] + " *\\([^)]*\\) *;");
Match m1 = r1.Match(ai);
if (m1.Success) {
ai = r1.Replace(ai, sm);
}
}
}
ai = ai.Replace(sd.aiName + "::", "");
ss += ai + "\n";
}
}
if (ss.Length != 0) {
string typeName = "UnknownScript";
switch (sd.type) {
case 1: typeName = "CreatureScript"; break;
case 2: typeName = "InstanceMapScript"; break;
default:
if (sd.name.IndexOf("npc") == 0)
typeName = "CreatureScript";
else if (sd.name.IndexOf("mob") == 0)
typeName = "CreatureScript";
else if (sd.name.IndexOf("boss_") == 0)
typeName = "CreatureScript";
else if (sd.name.IndexOf("item_") == 0)
typeName = "ItemScript";
else if (sd.name.IndexOf("go_") == 0)
typeName = "GameObjectScript";
else if (sd.name.IndexOf("at_") == 0)
typeName = "AreaTriggerScript";
else if (sd.name.IndexOf("instance_") == 0)
typeName = "InstanceMapScript";
break;
}
if (sd.instanceName != null)
ss = ss.Replace(sd.instanceName, sd.instanceName + "_InstanceMapScript");
ss = ss.Replace("\n", "\n ");
ss = "class " + sd.name + " : public " + typeName + "\n{\npublic:\n " +
sd.name + "() : " + typeName + "(\"" + sd.name + "\") { }\n" + ss + "\n};";
ss = ss.Replace("_" + sd.name, "");
ss = ss.Replace("AIAI", "AI");
ss = ss.Replace(" \r\n", "\r\n");
ss = ss.Replace(" \n", "\n");
txt = txt.Insert(minPos, ss);
register = " new " + sd.name + "();\n" + register;
}
}
Regex r2 = new Regex("void +AddSC_([_a-zA-Z0-9]+)");
Match m2 = r2.Match(txt);
if (m2.Success) {
txt = txt.Remove(m2.Index);
txt += "void AddSC_" + m2.Groups[1].Value + "()\n{\n" + register + "}\n";
}
// File.Copy(filePath, filePath + ".bkp");
txt = txt.Replace("\r\n", "\n");
File.WriteAllText(filePath, txt);
}
}
}
}

View File

@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ScriptConverter")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ScriptConverter")]
[assembly: AssemblyCopyright("Copyright © 2010")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("39b64a0a-a460-4177-925f-b9bdda4f2a1d")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{AB4C10BF-53AD-44AC-8B7A-B3F406DF468A}</ProjectGuid>
<OutputType>Exe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ScriptConverter</RootNamespace>
<AssemblyName>ScriptConverter</AssemblyName>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@@ -0,0 +1,6 @@
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<StartArguments>
</StartArguments>
</PropertyGroup>
</Project>