Skip to content

Commit

Permalink
简繁转换
Browse files Browse the repository at this point in the history
  • Loading branch information
lunzhiPenxil committed Dec 20, 2020
1 parent be01b73 commit b9bca1c
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 12 deletions.
140 changes: 140 additions & 0 deletions Dice/ChineseLocalization.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* ______ _____ ____ ____ ________
* |_ _ `.|_ _||_ _||_ _||_ __ |
* | | `. \ | | \ \ / / | |_ \_|
* | | | | | | > `' < | _| _
* _| |_.' /_| |_ _/ /'`\ \_ _| |__/ |
* |______.'|_____||____||____||________|
*
* OlivaDice(DIXE) QQ Dice Robot for TRPG
* Copyright (C) 2019-2020 lunzhiPenxil仑质
*
*/

#include <string>
#include <vector>
#include <map>
#include <cassert>
#include <Windows.h>
#include "EncodingConvert.h"
#include "ChineseLocalization.h"
#include "STLExtern.hpp"

#define CP_GBK (936)
#define CP_BIG5 (950)

#define WIN32_LEAN_AND_MEAN

//繁体到简体
string TCNGBKtoSCNGBK(const string _SrcString)
{
string _DstText;
char* pszGbt_old = NULL;
wchar_t* wszUnicode = NULL;
char* pszGbt = NULL;
char* pszGbs = NULL;
string sGb;
int iLen = 0;
pszGbt_old = (char*)((LPCSTR)_SrcString.c_str());
iLen = MultiByteToWideChar(CP_GBK, 0, pszGbt_old, -1, NULL, 0);
wszUnicode = new wchar_t[iLen + 1];
MultiByteToWideChar(CP_GBK, 0, pszGbt_old, -1, wszUnicode, iLen);
iLen = WideCharToMultiByte(CP_GBK, 0, (PWSTR)wszUnicode, -1, NULL, 0, NULL, NULL);
pszGbt = new char[iLen + 1];
pszGbs = new char[iLen + 1];
WideCharToMultiByte(CP_GBK, 0, (PWSTR)wszUnicode, -1, pszGbt, iLen, NULL, NULL);
LCMapString(0x0804, LCMAP_SIMPLIFIED_CHINESE, pszGbt, -1, pszGbs, iLen);
sGb = pszGbs;
delete[] wszUnicode;
delete[] pszGbt;
delete[] pszGbs;

_DstText = string(sGb.c_str());
return _DstText;
}

//简体到繁体
string SCNGBKtoTCNGBK(const string _SrcString)
{
string _DstText;
char* pszGbt_old = NULL;
wchar_t* wszUnicode = NULL;
char* pszGbt = NULL;
char* pszGbs = NULL;
string sGb;
int iLen = 0;
pszGbt_old = (char*)((LPCSTR)_SrcString.c_str());
iLen = MultiByteToWideChar(CP_GBK, 0, pszGbt_old, -1, NULL, 0);
wszUnicode = new wchar_t[iLen + 1];
MultiByteToWideChar(CP_GBK, 0, pszGbt_old, -1, wszUnicode, iLen);
iLen = WideCharToMultiByte(CP_GBK, 0, (PWSTR)wszUnicode, -1, NULL, 0, NULL, NULL);
pszGbt = new char[iLen + 1];
pszGbs = new char[iLen + 1];
WideCharToMultiByte(CP_GBK, 0, (PWSTR)wszUnicode, -1, pszGbt, iLen, NULL, NULL);
LCMapString(0x0804, LCMAP_TRADITIONAL_CHINESE, pszGbt, -1, pszGbs, iLen);
sGb = pszGbs;
delete[] wszUnicode;
delete[] pszGbt;
delete[] pszGbs;

_DstText = string(sGb.c_str());
return _DstText;
}


//完整编码转换
//繁体到简体
string TCNBIG5toSCNGBK(const string _SrcString)
{
string _DstText;
char* pszBig5 = NULL;
wchar_t* wszUnicode = NULL;
char* pszGbt = NULL;
char* pszGbs = NULL;
string sGb;
int iLen = 0;
pszBig5 = (char*)((LPCSTR)_SrcString.c_str());
iLen = MultiByteToWideChar(CP_BIG5, 0, pszBig5, -1, NULL, 0);
wszUnicode = new wchar_t[iLen + 1];
MultiByteToWideChar(CP_BIG5, 0, pszBig5, -1, wszUnicode, iLen);
iLen = WideCharToMultiByte(CP_GBK, 0, (PWSTR)wszUnicode, -1, NULL, 0, NULL, NULL);
pszGbt = new char[iLen + 1];
pszGbs = new char[iLen + 1];
WideCharToMultiByte(CP_GBK, 0, (PWSTR)wszUnicode, -1, pszGbt, iLen, NULL, NULL);
LCMapString(0x0804, LCMAP_SIMPLIFIED_CHINESE, pszGbt, -1, pszGbs, iLen);
sGb = pszGbs;
delete[] wszUnicode;
delete[] pszGbt;
delete[] pszGbs;

_DstText = string(sGb.c_str());
return _DstText;
}

//简体到繁体
string SCNGBKtoTCNBIG5(const string _SrcString)
{
string _DstText;
char* pszGbt = NULL;
char* pszGbs = NULL;
wchar_t* wszUnicode = NULL;
char* pszBig5 = NULL;
string sBig5;
int iLen = 0;
pszGbs = (char*)((LPCSTR)_SrcString.c_str());
iLen = MultiByteToWideChar(CP_GBK, 0, pszGbs, -1, NULL, 0);
pszGbt = new char[iLen * 2 + 1];
LCMapString(0x0804, LCMAP_TRADITIONAL_CHINESE, pszGbs, -1, pszGbt, iLen * 2);
wszUnicode = new wchar_t[iLen + 1];
MultiByteToWideChar(CP_GBK, 0, pszGbt, -1, wszUnicode, iLen);
iLen = WideCharToMultiByte(CP_BIG5, 0, (PWSTR)wszUnicode, -1, NULL, 0, NULL, NULL);
pszBig5 = new char[iLen + 1];
WideCharToMultiByte(CP_BIG5, 0, (PWSTR)wszUnicode, -1, pszBig5, iLen, NULL, NULL);
sBig5 = pszBig5;
delete[] wszUnicode;
delete[] pszGbt;
delete[] pszBig5;

_DstText = string(sBig5.c_str());
return _DstText;
}
22 changes: 22 additions & 0 deletions Dice/ChineseLocalization.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* ______ _____ ____ ____ ________
* |_ _ `.|_ _||_ _||_ _||_ __ |
* | | `. \ | | \ \ / / | |_ \_|
* | | | | | | > `' < | _| _
* _| |_.' /_| |_ _/ /'`\ \_ _| |__/ |
* |______.'|_____||____||____||________|
*
* OlivaDice(DIXE) QQ Dice Robot for TRPG
* Copyright (C) 2019-2020 lunzhiPenxilÂØÖÊ
*
*/

#include <string>
#include <map>
#include "STLExtern.hpp"


string TCNGBKtoSCNGBK(const string _SrcString);
string SCNGBKtoTCNGBK(const string _SrcString);
string TCNBIG5toSCNGBK(const string _SrcString);
string SCNGBKtoTCNBIG5(const string _SrcString);
1 change: 1 addition & 0 deletions Dice/Dice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
#include "S3PutObject.h"
#include "DiceCensor.h"
#include "DiceLua.h"
#include "ChineseLocalization.h"

#pragma warning(disable:4996)
#pragma warning(disable:6031)
Expand Down
2 changes: 2 additions & 0 deletions Dice/Dice.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
<ClCompile Include="BlackListManager.cpp" />
<ClCompile Include="CardDeck.cpp" />
<ClCompile Include="CharacterCard.cpp" />
<ClCompile Include="ChineseLocalization.cpp" />
<ClCompile Include="Dice.cpp" />
<ClCompile Include="DiceCensor.cpp" />
<ClCompile Include="DiceCloud.cpp" />
Expand Down Expand Up @@ -236,6 +237,7 @@
<ClInclude Include="BlackListManager.h" />
<ClInclude Include="CardDeck.h" />
<ClInclude Include="CharacterCard.h" />
<ClInclude Include="ChineseLocalization.h" />
<ClInclude Include="DiceCensor.h" />
<ClInclude Include="DiceCloud.h" />
<ClInclude Include="DiceConsole.h" />
Expand Down
6 changes: 6 additions & 0 deletions Dice/Dice.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@
<ClCompile Include="Lua\linit.c">
<Filter>源文件\Lua</Filter>
</ClCompile>
<ClCompile Include="ChineseLocalization.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="APPINFO.h">
Expand Down Expand Up @@ -503,6 +506,9 @@
<ClInclude Include="DiceLua.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="ChineseLocalization.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
13 changes: 2 additions & 11 deletions Dice/DiceEvent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <memory>
#include "Lua/lua.hpp"
#include "DiceLua.h"
#include "ChineseLocalization.h"

//#pragma warning(disable:28159)
using namespace std;
Expand Down Expand Up @@ -2096,13 +2097,7 @@ int FromMsg::DiceReply()
intMsgCnt++;
vector<string> ProDeck;
vector<string>* TempDeck = nullptr;
//bool isPrivate(false);
string& key{ strVar["deck_name"] = readAttrName() };
if (!strVar["deck_name"].empty() && strVar["deck_name"][0] == '_') {
isPrivate = true;
strVar["hidden"];
strVar["deck_name"].erase(strVar["deck_name"].begin());
}
if (strVar["deck_name"].empty()){
reply(fmt->get_help("draw"));
return 1;
Expand Down Expand Up @@ -3353,11 +3348,6 @@ int FromMsg::DiceReply()
? get(chat(fromGroup).intConf, string("rc房规"), 0)
: get(getUser(fromQQ).intConf, string("rc房规"), 0);
int intTurnCnt = 1;
//bool isHidden(false);
if (strMsg[intMsgCnt] == '_') {
isHidden = true;
++intMsgCnt;
}
if (strMsg.find('#') != string::npos)
{
string strTurnCnt = strMsg.substr(intMsgCnt, strMsg.find('#') - intMsgCnt);
Expand Down Expand Up @@ -3406,6 +3396,7 @@ int FromMsg::DiceReply()
strVar["attr"] = strMsg.substr(intMsgCnt);
if (PList.count(fromQQ) && PList[fromQQ][fromGroup].count(strVar["attr"]))intMsgCnt = strMsg.length();
else strVar["attr"] = readAttrName();
strVar["attr"] = TCNGBKtoSCNGBK(strVar["attr"]);
if (strVar["attr"].find("自动成功") == 0)
{
strDifficulty = strVar["attr"].substr(0, 8);
Expand Down
2 changes: 1 addition & 1 deletion Dice/DiceLua.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ int Dice_FGetJson(lua_State* L)
{
if (lua_isinteger(L, i))
{
int this_obj = lua_tointeger(L, i);
unsigned int this_obj = lua_tointeger(L, i);
rv_json_path += dot + "[" + to_string(this_obj) + "]";
if (obj_json.size() < this_obj)
{
Expand Down

0 comments on commit b9bca1c

Please sign in to comment.