Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code change for definition name case mismatches #10

Open
BearSleepy opened this issue Jun 18, 2017 · 0 comments
Open

Code change for definition name case mismatches #10

BearSleepy opened this issue Jun 18, 2017 · 0 comments

Comments

@BearSleepy
Copy link

BearSleepy commented Jun 18, 2017

I had this issue last year, just sharing my changes. There are probably regular people that never realize why.

Also there are issues running from other programs. Like DascView which uses the windows associated app mechanism, effectively launches with working directory of system32, like "c:\Windows\System32" which is less than ideal for lots of reasons.

In MainForm.cs

        var definitions = m_definitions.Tables.Where(t => (t.Name.Equals(m_dbcName, StringComparison.OrdinalIgnoreCase)));
        //var definitions = m_definitions.Tables.Where(t => t.Name == m_dbcName);

        if (!definitions.Any())
        {
            definitions = m_definitions.Tables.Where(t => t.Name.Equals(Path.GetFileName(m_dbcFile), StringComparison.OrdinalIgnoreCase));
            //definitions = m_definitions.Tables.Where(t => t.Name == Path.GetFileName(m_dbcFile));
        }

In Program.cs

internal static class Program
{
	internal static string OrignalWorkingDirectory { get; set; }
	internal static string ApplicationDirectory { get; set; }
    
	/// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
	{
		OrignalWorkingDirectory = Directory.GetCurrentDirectory();
		ApplicationDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

		// Allow callers to not need to specify our app directory as starting directory.
		// CascView for example will not set this and will cause DBCViewer to fail to find definitions.
		Directory.SetCurrentDirectory(ApplicationDirectory);

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
    }
}

You can have this too. It allows a generic Lua output. You know where this file is I'm sure ;)

using PluginInterface;
 using System;
 using System.Collections.Generic;
 using System.ComponentModel.Composition;
 using System.Data;
 using System.IO;
 using System.Text;
 using System.Text.RegularExpressions;

 namespace ExportFileDataAsLuaTable
 {
    [Export(typeof(IPlugin))]
    public class FileDataLuaExporter : IPlugin
    {
        [Import("PluginFinished")]
        public Action<int> Finished { get; set; }
        [Import("ClearDataTable")]
        public Action ClearDataTable { get; set; }

        public void Run(DataTable table)
        {
            if (table.TableName == "SoundKit.db2")
            {
                using (var sw1 = new StreamWriter("SoundKitData.lua"))
                {
                    sw1.WriteLine("local SOUNDDATA = {");

                    foreach (DataRow row in table.Rows)
                    {
                        sw1.WriteLine("[{0}] = \"{1}\"", row[16], row[0]);
                    }

                    sw1.WriteLine("}");
                }

                return;
            }

			if (table.TableName == "ManifestInterfaceData.db2")
			{
				using (var sw = new StreamWriter("ManifestInterfaceData.txt"))
				{
					foreach (DataRow row in table.Rows)
					{
						sw.WriteLine("{0}{1}", row[1], row[2]);
					}
				}
			}

			var fileName = Path.GetFileNameWithoutExtension(table.TableName) + ".lua";
			using (var sw = new StreamWriter(fileName))
			{
				RunGeneric(table, sw);
			}
        }

		string FilterToLuaText(string s)
		{
			if (s.IndexOf('\n') != -1 || s.IndexOf('\r') != -1)
				s = Regex.Replace(s, @"\r\n?|\n", "\\n");

			if (s.IndexOf('\"') != -1)	// assume caller will use double quotes if enclosing
				s = Regex.Replace(s, "\\\"", "\\\"");

			return s;
		}

		public void RunGeneric(DataTable table, StreamWriter sw)
		{
			var tableName = Path.GetFileNameWithoutExtension(table.TableName);

			sw.WriteLine("-- " + tableName);
			sw.WriteLine("");

			sw.WriteLine(@"db_tablecols = db_tablecols or {}; db_tablecols['" + tableName + "'] = {");
			foreach (DataColumn col in table.Columns)
			{
				sw.Write("\"" + FilterToLuaText(col.Caption) + "\", ");
			}
			sw.WriteLine("}");
			sw.WriteLine("");

			sw.WriteLine(@"db_tables = db_tables or {}; db_tables['" + tableName + "'] = {");
            foreach (DataRow row in table.Rows)
            {
				sw.Write("{");
				foreach (var item in row.ItemArray)
				{
					var s = item as String;
					if ((object)s != null)
						s = "\"" + FilterToLuaText(s) + "\", ";
					else if ((object)item != null)
						s = item.ToString() + ", ";
					else
						s = " , ";
					sw.Write(s);
				}
				sw.WriteLine("},");
            }
			sw.WriteLine("}");
			sw.WriteLine("");
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant