pointer fix random crash & better substring logic

This commit is contained in:
Ade Novan 2016-10-23 00:06:07 +08:00 committed by Gde Made Novan Priambhada
parent 08a1cc7075
commit 07b1d8b845
5 changed files with 65 additions and 39 deletions

View File

@ -56,7 +56,7 @@ SceNetAdhocPdpStat * pdp[255];
SceNetAdhocPtpStat * ptp[255];
uint32_t localip;
std::vector<std::string> chatLog;
ChatMenu* ch;
ChatMenu * ch;
std::string name = "";
std::string incoming = "";
std::string message = "";
@ -986,7 +986,8 @@ void freeFriendsRecursive(SceNetAdhocctlPeerInfo * node) {
free(node);
}
void setChatPointer(ChatMenu* chatmenu) {
//@params chatmenu pass NULL on Hide , and pass ChatMenu On Create (EmuScreen.cpp)
void setChatPointer(ChatMenu * chatmenu) {
//set chatscreen instance
ch = chatmenu;
}
@ -994,24 +995,28 @@ void setChatPointer(ChatMenu* chatmenu) {
void sendChat(std::string chatString) {
SceNetAdhocctlChatPacketC2S chat;
chat.base.opcode = OPCODE_CHAT;
//Send Chat Messages need to check network inited and already connected to server or not
//if (friendFinderRunning)
//TODO check network inited, check send success or not, chatlog.pushback error on failed send, pushback error on not connected
//if (friendFinderRunning) //need to check network inited and already connected to server or not is this correct?
//{
// Send Chat to Server
//maximum char allowed is 64 character for compability with original server (pro.coldbird.net)
message = chatString.substr(0, 57); // 64 return chat variable corrupted is it out of memory?
strcpy(chat.message, message.c_str());
NOTICE_LOG(SCENET, "Send %s to Adhoc Server", chat.message);
int chatResult = send(metasocket, (const char *)&chat, sizeof(chat), 0);
//need to check if send success or not before appending to chat screen
name = g_Config.sNickName.c_str();
chatLog.push_back(name.substr(0, 8) + ": " + chat.message);
// Send Chat to Server
//maximum char allowed is 64 character for compability with original server (pro.coldbird.net)
message = chatString.substr(0, 60); // 64 return chat variable corrupted is it out of memory?
strcpy(chat.message, message.c_str());
//Send Chat Messages
int chatResult = send(metasocket, (const char *)&chat, sizeof(chat), 0);
NOTICE_LOG(SCENET, "Send %s to Adhoc Server", chat.message);
name = g_Config.sNickName.c_str();
chatLog.push_back(name.substr(0, 8) + ": " + chat.message);
//}
}
std::vector<std::string> getChatLog() {
// this log used by chat screen
if (chatLog.size() > 50) {
//erase the first 40 element limit the chatlog size
chatLog.erase(chatLog.begin(), chatLog.begin() + 40);
}
return chatLog;
}

View File

@ -847,7 +847,7 @@ void addFriend(SceNetAdhocctlConnectPacketS2C * packet);
* Send chat or get that
* @param std::string ChatString
*/
void setChatPointer(ChatMenu* chatmenu);
void setChatPointer(ChatMenu * chatmenu);
void sendChat(std::string chatString);
std::vector<std::string> getChatLog();

View File

@ -8,20 +8,19 @@
#include "Common/LogManager.h"
#include "Core/HLE/proAdhoc.h"
#include "i18n/i18n.h"
#include <ctype.h>
void ChatMenu::CreatePopupContents(UI::ViewGroup *parent) {
using namespace UI;
//tried to give instance to proAdhoc not working
//setChatPointer(this);
I18NCategory *n = GetI18NCategory("Networking");
LinearLayout *outer = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, 400));
scroll_ = outer->Add(new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(PopupWidth(), FILL_PARENT, 1.0f)));
chatVert_ = scroll_->Add(new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(PopupWidth(), WRAP_CONTENT)));
chatVert_->SetSpacing(0);
LinearLayout *outer = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT,400));
scroll_ = outer->Add(new ScrollView(ORIENT_VERTICAL, new LinearLayoutParams(1.0)));
LinearLayout *bottom = outer->Add(new LinearLayout(ORIENT_HORIZONTAL, new LayoutParams(FILL_PARENT, WRAP_CONTENT)));
chatEdit_ = bottom->Add(new TextEdit("", n->T("Chat Here"), new LinearLayoutParams(1.0)));
chatEdit_->OnEnter.Handle(this, &ChatMenu::OnSubmit);
bottom->Add(new Button(n->T("Send")))->OnClick.Handle(this, &ChatMenu::OnSubmit);
chatVert_ = scroll_->Add(new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)));
chatVert_->SetSpacing(0);
parent->Add(outer);
UpdateChat();
}
@ -69,36 +68,47 @@ UI::EventReturn ChatMenu::OnSubmit(UI::EventParams &e) {
return UI::EVENT_DONE;
}
std::vector<std::string> Split(const std::string& str, int splitLength)
std::vector<std::string> Split(const std::string& str)
{
int NumSubstrings = str.length() / splitLength;
std::vector<std::string> ret;
// TODO sub string in white space
for (auto i = 0; i < NumSubstrings; i++)
{
ret.push_back(str.substr(i * splitLength, splitLength));
int counter = 0;
int firstSentenceEnd = 0;
int secondSentenceEnd = 0;
NOTICE_LOG(HLE, "Splitted %s %i", str.c_str(),str.size());
for (auto i = 0; i<str.length(); i++) {
if (isspace(str[i])) {
if (i < 35) {
if(str[i-1]!=':')
firstSentenceEnd = i+1;
}
else if (i > 35) {
secondSentenceEnd = i;
}
}
}
// If there are leftover characters, create a shorter item at the end.
if (str.length() % splitLength != 0)
{
ret.push_back(str.substr(splitLength * NumSubstrings));
if (firstSentenceEnd == 0) {
firstSentenceEnd = 35;
}
if(secondSentenceEnd == 0){
secondSentenceEnd = str.length();
}
ret.push_back(str.substr(0, firstSentenceEnd));
ret.push_back(str.substr(firstSentenceEnd, secondSentenceEnd));
return ret;
}
void ChatMenu::UpdateChat() {
using namespace UI;
if (chatVert_ != NULL) {
chatVert_->Clear();
chatVert_->Clear(); //read Access violation is proadhoc.cpp use NULL_->Clear() pointer?
std::vector<std::string> chatLog = getChatLog();
for (auto i : chatLog) {
if (i.length() > 30) {
//split long text
std::vector<std::string> splitted = Split(i, 32);
std::vector<std::string> splitted = Split(i);
for (auto j : splitted) {
TextView *v = chatVert_->Add(new TextView(j, FLAG_DYNAMIC_ASCII, false, new LayoutParams(PopupWidth(), WRAP_CONTENT)));
uint32_t color = 0xFFFFFF;
@ -111,7 +121,7 @@ void ChatMenu::UpdateChat() {
v->SetTextColor(0xFF000000 | color);
}
}
scroll_->ScrollToBottom();
toBottom_ = true;
}
}
@ -120,8 +130,18 @@ bool ChatMenu::touch(const TouchInput &touch) {
return UIDialogScreen::touch(touch);
}
if (!box_->GetBounds().Contains(touch.x, touch.y))
if (!box_->GetBounds().Contains(touch.x, touch.y)){
screenManager()->finishDialog(this, DR_BACK);
setChatPointer(NULL); //fix the random crash
}
return UIDialogScreen::touch(touch);
}
void ChatMenu::update(InputState &input) {
if (toBottom_) {
toBottom_ = false;
scroll_->ScrollToBottom();
}
PopupScreen::update(input);
}

View File

@ -9,6 +9,7 @@ public:
void CreateViews() override;
void dialogFinished(const Screen *dialog, DialogResult result) override;
bool touch(const TouchInput &touch) override;
void update(InputState &input) override;
void UpdateChat();
private:
UI::EventReturn OnSubmit(UI::EventParams &e);

View File

@ -87,7 +87,6 @@ AVIDump avi;
static bool frameStep_;
static int lastNumFlips;
static bool startDumping;
static void __EmuScreenVblank()
{
I18NCategory *sy = GetI18NCategory("System");
@ -779,8 +778,9 @@ void EmuScreen::CreateViews() {
if (g_Config.bShowDeveloperMenu) {
root_->Add(new Button("DevMenu"))->OnClick.Handle(this, &EmuScreen::OnDevTools);
}
if (g_Config.bEnableNetworkChat)
if (g_Config.bEnableNetworkChat) {
root_->Add(new Button(sc->T("Chat"), new AnchorLayoutParams(50, NONE, NONE, 50, true)))->OnClick.Handle(this, &EmuScreen::OnChat);
}
saveStatePreview_ = new AsyncImageFileView("", IS_FIXED, nullptr, new AnchorLayoutParams(bounds.centerX(), 100, NONE, NONE, true));
saveStatePreview_->SetFixedSize(160, 90);
saveStatePreview_->SetColor(0x90FFFFFF);
@ -801,7 +801,7 @@ UI::EventReturn EmuScreen::OnDevTools(UI::EventParams &params) {
UI::EventReturn EmuScreen::OnChat(UI::EventParams &params) {
releaseButtons();
ChatMenu* ch = new ChatMenu();
ChatMenu * ch = new ChatMenu(); // memory leak or not?
setChatPointer(ch);
screenManager()->push(ch);
return UI::EVENT_DONE;