<filename>ProtoToLua/csv2lua.py
# -*- coding: utf-8 -*-
#!/usr/bin/python3

import configparser
import os
import os.path
import pprint
import re
import csv
import codecs

class MsgInfo(object):
	def __init__(self,msgid,msgname,comment):
		self.msgid = msgid
		self.msgname = msgname
		self.comment = comment
		
	msgid = ""
	msgname = ""
	comment = ""
class WrapFile:
	fs = None
	def __init__(self,real_file):
		self.fs = real_file
	def writelines(self,s):
		self.fs.write(s + "\n")
	def flush(self):
		self.fs.flush()
	def close(self):
		self.fs.close()

def ParseDefine(fs,msgidList):
	fs.writelines("--Generated By csv2lua Do not Edit");
	fs.writelines("local config = {");

	for _msgDef in msgidList:
		fs.writelines("\t%s = %s, --%s"%( _msgDef.msgname, _msgDef.msgid,_msgDef.comment));

	fs.writelines("}");
	fs.writelines("return config");
	fs.flush();
	fs.close();

def GenDefine():
	begin_path = os.getcwd()
	config=configparser.ConfigParser()
	config.read("Config.ini")
	csvRootPath =  config.get("DataPath","dataPath")
	luaRootPath = config.get("DataPath","tableluaPath")
	luaUIIDRootPath = config.get("DataPath","tableUIIDluaPath")

	csvPath = csvRootPath + "ClientTables\\Hint.csv"
	luaPath = luaRootPath + "HintIDDefine.lua"
	contextList = LoadHintCSV(csvPath)
	Writelua(csvPath,luaPath,contextList)
	
	csvPath = csvRootPath + "ClientTables\\UIData.csv"
	luaPath = luaUIIDRootPath + "UIID.lua"
	contextList = LoadUIIDCSV(csvPath)
	Writelua(csvPath,luaPath,contextList)

	csvPath = csvRootPath + "ClientTables\\EffectConfig.csv"
	luaPath = luaRootPath + "EffectIDDefine.lua"
	contextList = LoadEffectCSV(csvPath)
	Writelua(csvPath,luaPath,contextList)

	csvPath = csvRootPath + "PublicTables\\GlobalVariables.csv"
	luaPath = luaRootPath + "GlobalVarIDDefine.lua"
	contextList = LoadGlobalvalueCSV(csvPath)
	Writelua(csvPath,luaPath,contextList)

	csvPath = csvRootPath + "ClientTables\\AudioResource.csv"
	luaPath = luaRootPath + "AudioResourceIDDefine.lua"
	contextList = LoadAudioResourceCSV(csvPath)
	Writelua(csvPath,luaPath,contextList)
	
	csvPath = csvRootPath + "ClientTables\\CommonComponent.csv"
	luaPath = luaRootPath + "UITipsIDDefine.lua"
	contextList = LoadCommonComponentCSV(csvPath)
	Writelua(csvPath,luaPath,contextList)
	

def Writelua(csvPath,luaPath,contextList):
	print("START GEN" + luaPath)
	fs = WrapFile(open(luaPath,"w+",-1,"utf-8"))
	ParseDefine(fs,contextList)
	print(luaPath + " done")


def LoadHintCSV(csvpath):
	csvContextList = []
	with open(csvpath) as csvfile:
		csvreader = csv.reader(csvfile)
		next(csvreader)
		next(csvreader)
		next(csvreader)
		next(csvreader)

		for row in csvreader:
			csvContextList.append(MsgInfo(row[0],row[1],row[4]))
	return csvContextList	
	
def LoadUIIDCSV(csvpath):
	csvContextList = []
	with open(csvpath) as csvfile:
		csvreader = csv.reader(csvfile)
		next(csvreader)
		next(csvreader)
		next(csvreader)
		next(csvreader)

		for row in csvreader:
			textname = row[3] + "_" + row[0]
			csvContextList.append(MsgInfo(row[0], textname, row[3]))
	return csvContextList	

def LoadEffectCSV(csvpath):
	csvContextList = []
	with open(csvpath) as csvfile:
		csvreader = csv.reader(csvfile)
		next(csvreader)
		next(csvreader)
		next(csvreader)
		next(csvreader)

		for row in csvreader:
			csvContextList.append(MsgInfo(row[0],row[1],row[9]))
	return csvContextList	

def LoadGlobalvalueCSV(csvpath):
	csvContextList = []
	with open(csvpath) as csvfile:
		csvreader = csv.reader(csvfile)
		next(csvreader)
		next(csvreader)
		next(csvreader)
		next(csvreader)

		for row in csvreader:
			csvContextList.append(MsgInfo(row[0],row[2],row[5]))
	return csvContextList	

def LoadAudioResourceCSV(csvpath):
	csvContextList = []
	with open(csvpath) as csvfile:
		csvreader = csv.reader(csvfile)
		next(csvreader)
		next(csvreader)
		next(csvreader)
		next(csvreader)

		for row in csvreader:
			if row[0] != row[1]:
				csvContextList.append(MsgInfo(row[0],row[1],row[8]))
	return csvContextList	

def LoadCommonComponentCSV(csvpath):
	csvContextList = []
	with open(csvpath) as csvfile:
		csvreader = csv.reader(csvfile)
		next(csvreader)
		next(csvreader)
		next(csvreader)
		next(csvreader)

		for row in csvreader:
			csvContextList.append(MsgInfo(row[0],row[1],row[2]))
	return csvContextList	

GenDefine()
