chatstack & textfile
This commit is contained in:
parent
f2bb4ef155
commit
9d2a19fdae
@ -9,39 +9,28 @@
|
||||
#include "common.h"
|
||||
#include "sdk.h"
|
||||
|
||||
ChatStack::ChatStack() {
|
||||
m_Buffer = new char[256 * CHATSTACK_SIZE];
|
||||
m_nStackDepth = 0;
|
||||
namespace chat_stack {
|
||||
|
||||
void OnCreateMove() {
|
||||
if (last_say > g_GlobalVars->curtime) last_say = 0;
|
||||
if (g_GlobalVars->curtime - CHATSTACK_INTERVAL <= last_say) return;
|
||||
std::string message;
|
||||
if (!stack.empty()) {
|
||||
message = stack.top();
|
||||
stack.pop();
|
||||
if (message.size()) {
|
||||
g_IEngine->ServerCmd(format("say \"", message, '"').c_str());
|
||||
last_say = g_GlobalVars->curtime;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ChatStack::~ChatStack() {
|
||||
delete [] m_Buffer;
|
||||
void Reset() {
|
||||
while (!stack.empty()) stack.pop();
|
||||
last_say = 0.0f;
|
||||
}
|
||||
|
||||
void ChatStack::OnCreateMove() {
|
||||
if (m_nStackDepth <= 0) return;
|
||||
if (m_fLastSay > g_GlobalVars->curtime) m_fLastSay = 0;
|
||||
if (g_GlobalVars->curtime - CHATSTACK_INTERVAL <= m_fLastSay) return;
|
||||
const char* msg = Pop();
|
||||
const char* cmd = strfmt("say \"%s\"", msg);
|
||||
g_IEngine->ServerCmd(cmd);
|
||||
m_fLastSay = g_GlobalVars->curtime;
|
||||
delete [] cmd;
|
||||
std::stack<std::string> stack;
|
||||
float last_say = 0.0f;
|
||||
|
||||
}
|
||||
|
||||
void ChatStack::Push(const char* message) {
|
||||
if (m_nStackDepth >= CHATSTACK_SIZE) return;
|
||||
strncpy(&m_Buffer[m_nStackDepth++ * 256], message, 255);
|
||||
}
|
||||
|
||||
const char* ChatStack::Pop() {
|
||||
if (m_nStackDepth <= 0) return 0;
|
||||
return (const char*)&m_Buffer[(--m_nStackDepth) * 256];
|
||||
}
|
||||
|
||||
|
||||
void ChatStack::Reset() {
|
||||
m_nStackDepth = 0;
|
||||
}
|
||||
|
||||
ChatStack* g_pChatStack = 0;
|
||||
|
@ -8,22 +8,22 @@
|
||||
#ifndef CHATSTACK_H_
|
||||
#define CHATSTACK_H_
|
||||
|
||||
#define CHATSTACK_SIZE 32
|
||||
#define CHATSTACK_INTERVAL 0.8f
|
||||
|
||||
class ChatStack {
|
||||
public:
|
||||
ChatStack();
|
||||
~ChatStack();
|
||||
void OnCreateMove();
|
||||
void Reset();
|
||||
void Push(const char* message);
|
||||
const char* Pop();
|
||||
int m_nStackDepth;
|
||||
char* m_Buffer;
|
||||
float m_fLastSay;
|
||||
#include "beforecheaders.h"
|
||||
#include <string>
|
||||
#include <stack>
|
||||
#include <functional>
|
||||
#include "aftercheaders.h"
|
||||
|
||||
namespace chat_stack {
|
||||
|
||||
void OnCreateMove();
|
||||
void Reset();
|
||||
|
||||
extern std::stack<std::string> stack;
|
||||
extern float last_say;
|
||||
|
||||
};
|
||||
|
||||
extern ChatStack* g_pChatStack;
|
||||
|
||||
#endif /* CHATSTACK_H_ */
|
||||
|
@ -6,52 +6,39 @@
|
||||
*/
|
||||
|
||||
#include "textfile.h"
|
||||
|
||||
#include "common.h"
|
||||
|
||||
#include "beforecheaders.h"
|
||||
#include <fstream>
|
||||
#include "aftercheaders.h"
|
||||
|
||||
#include <pwd.h>
|
||||
#include <stdio.h>
|
||||
|
||||
TextFile::TextFile(unsigned length, unsigned lines) {
|
||||
m_nCount = lines;
|
||||
m_nLength = length;
|
||||
m_Buffer = new char[length * lines];
|
||||
}
|
||||
|
||||
TextFile::~TextFile() {
|
||||
delete [] m_Buffer;
|
||||
}
|
||||
|
||||
void TextFile::LoadFile(const char* name) {
|
||||
void TextFile::Load(std::string name) {
|
||||
uid_t uid = geteuid();
|
||||
passwd* pw = getpwuid(uid);
|
||||
if (!pw) {
|
||||
logging::Info("can't get the username!");
|
||||
return;
|
||||
}
|
||||
char* filename = strfmt("/home/%s/.cathook/%s", pw->pw_name, name);
|
||||
FILE* file = fopen(filename, "r");
|
||||
if (!file) {
|
||||
std::string filename = format("/home/", pw->pw_name, "/.cathook/", name);
|
||||
std::ifstream file(filename, std::ios::in);
|
||||
if (file.bad()) {
|
||||
logging::Info("Could not open the file: %s", filename);
|
||||
delete filename;
|
||||
return;
|
||||
}
|
||||
delete filename;
|
||||
char* buffer = new char[m_nLength];
|
||||
for (m_nCount = 0; m_nCount < m_nLines; m_nCount++) {
|
||||
if(fgets(buffer, m_nLength - 1, file)) {
|
||||
if (buffer[strlen(buffer) - 1] == '\n')
|
||||
buffer[strlen(buffer) - 1] = '\0';
|
||||
strncpy(&m_Buffer[m_nCount * m_nLength], buffer, m_nLength - 1);
|
||||
} else break;
|
||||
lines.clear();
|
||||
for (std::string line; std::getline(file, line);) {
|
||||
lines.push_back(line);
|
||||
}
|
||||
delete [] buffer;
|
||||
}
|
||||
|
||||
const char* TextFile::GetLine(unsigned line) {
|
||||
if (line < 0 || line >= m_nCount) return 0;
|
||||
return (const char*)&m_Buffer[line * m_nLength];
|
||||
size_t TextFile::LineCount() const {
|
||||
return lines.size();
|
||||
}
|
||||
|
||||
unsigned TextFile::GetLineCount() {
|
||||
return m_nCount;
|
||||
const std::string& TextFile::Line(size_t id) const {
|
||||
return lines.at(id);
|
||||
}
|
||||
|
@ -8,19 +8,18 @@
|
||||
#ifndef TEXTFILE_H_
|
||||
#define TEXTFILE_H_
|
||||
|
||||
#include "beforecheaders.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "aftercheaders.h"
|
||||
|
||||
class TextFile {
|
||||
public:
|
||||
TextFile(unsigned length, unsigned lines);
|
||||
~TextFile();
|
||||
void LoadFile(const char* name);
|
||||
unsigned GetLineCount();
|
||||
const char* GetLine(unsigned line);
|
||||
void Load(std::string filename);
|
||||
size_t LineCount() const;
|
||||
const std::string& Line(size_t id) const;
|
||||
|
||||
unsigned m_nLength;
|
||||
unsigned m_nLines;
|
||||
unsigned m_nCount;
|
||||
const char* m_pszFilename;
|
||||
char* m_Buffer;
|
||||
std::vector<std::string> lines;
|
||||
};
|
||||
|
||||
#endif /* TEXTFILE_H_ */
|
||||
|
Reference in New Issue
Block a user