Skip to content

Commit

Permalink
ystring update
Browse files Browse the repository at this point in the history
  • Loading branch information
slowcheet4h committed Oct 18, 2022
1 parent 7b801d3 commit 03dff35
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 205 deletions.
3 changes: 1 addition & 2 deletions pisi/unitedmeows/yystal/YYStal.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.io.IOException;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class YYStal {

Expand Down Expand Up @@ -191,7 +190,7 @@ public static <X> X setting(YSettings setting) {
return (X) settings.get(setting);
}

public static YString yString(String str) { return new YString(str); }
public static YString string(String str) { return new YString(str); }

public static Thread mainThread() {
return mainThread;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package pisi.unitedmeows.yystal.exception.impl.sql;

import pisi.unitedmeows.yystal.exception.YEx;

public class YexMySQLDriverError extends YEx {

private String message;

public YexMySQLDriverError(String error) {
message = error;
}

@Override
public String getMessage() {
return message;
}
}
17 changes: 17 additions & 0 deletions pisi/unitedmeows/yystal/exception/impl/sql/YexSqlError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package pisi.unitedmeows.yystal.exception.impl.sql;

import pisi.unitedmeows.yystal.exception.YEx;

public class YexSqlError extends YEx {

private String message;

public YexSqlError(final String _message) {
message = _message;
}

@Override
public String getMessage() {
return message;
}
}
76 changes: 71 additions & 5 deletions pisi/unitedmeows/yystal/hook/YString.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,24 @@
import pisi.unitedmeows.yystal.clazz.HookClass;

import java.lang.StringBuilder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class YString extends HookClass<StringBuilder> {

public static final String EMPTY_R = "";
public static final YString EMPTY = new YString("");

private static final Pattern INLINE_VAR_PATTERN
= Pattern.compile("\\#\\[([a-zA-Z_][a-zA-Z0-9_]*).([a-zA-Z_][a-zA-Z0-9_]*)(\\(\\))?\\]");

private boolean changed;
private String currentValue;

private List<Object> holders = new LinkedList<Object>();
private Map<String, Object> entities = new LinkedHashMap<>();

public YString(StringBuilder stringBuilder) {
hooked = stringBuilder;
changed = true;
Expand Down Expand Up @@ -103,10 +108,71 @@ public List<String> lines() {

@Override
public String toString() {
return currentValue();
String value = currentValue();
if (changed) {

while (true) {
Matcher matcher = INLINE_VAR_PATTERN.matcher(value);
if (!matcher.find())
break;
String entityName = matcher.group(1);
String variable = matcher.group(2);
final Object entity = entities.getOrDefault(entityName, null);
if (entity != null) {
try {
Object data;
if (matcher.group().contains("()")) {
data = entity.getClass().getDeclaredMethod(variable).invoke(entity);
} else {
data = entity.getClass().getDeclaredField(variable).get(entity);
}
value = matcher.replaceFirst(data.toString());
} catch (Exception e) {
e.printStackTrace();
value = matcher.replaceFirst("?");
}

}
}

StringBuilder builder = new StringBuilder();
int holderIndex = 0;
boolean escapeNext = false;
for (char character : value.toCharArray()) {

if (character == '\\')
escapeNext = true;
else {
if (character == '^') {
if (escapeNext)
builder.deleteCharAt(builder.length() - 1);
else {
builder.append(holders.get(holderIndex++));
continue;
}
}

escapeNext = false;
}

builder.append(character);
}
value = builder.toString();
}
return value;
}

protected String currentValue() {
return changed ? (currentValue = hooked.toString()) : currentValue;
}

public YString entity(String name, Object entity) {
entities.put(name, entity);
return this;
}

public YString put(Object data) {
holders.add(data);
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import pisi.unitedmeows.yystal.clazz.HookClass;
import pisi.unitedmeows.yystal.exception.YExManager;
import pisi.unitedmeows.yystal.exception.impl.YexIO;
import pisi.unitedmeows.yystal.networking.advanced.T;
import pisi.unitedmeows.yystal.networking.advanced.server.pipeline.YServerChannelDataIn;
import pisi.unitedmeows.yystal.networking.simple.server.YSocketClient;
import pisi.unitedmeows.yystal.utils.MemoryReader;
import pisi.unitedmeows.yystal.utils.MemoryWriter;
import pisi.unitedmeows.yystal.utils.kThread;
Expand All @@ -16,10 +14,6 @@
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class YSocketChannel extends HookClass<Socket> {

Expand Down
158 changes: 0 additions & 158 deletions pisi/unitedmeows/yystal/sql/YDatabaseClient.java

This file was deleted.

46 changes: 46 additions & 0 deletions pisi/unitedmeows/yystal/sql/YDatabaseWorker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package pisi.unitedmeows.yystal.sql;

import pisi.unitedmeows.yystal.clazz.HookClass;
import pisi.unitedmeows.yystal.clazz.prop;
import pisi.unitedmeows.yystal.exception.YExManager;
import pisi.unitedmeows.yystal.exception.impl.sql.YexSqlError;
import pisi.unitedmeows.yystal.hook.YString;

import java.sql.*;

public class YDatabaseWorker extends HookClass<YSQLClient> {

// connection = DriverManager.getConnection("jdbc:mysql://" + _host + ":" + _port + "/" + database + "?characterEncoding=latin1&useConfigs=maxPerformance", _username, _password);

public final prop<String> database;
private Connection connection;
private boolean connected;

protected YDatabaseWorker(final YSQLClient owner, final String _database) {
hooked = owner;
database = new prop<String>(_database);

try {
/* not required */ synchronized (this) {
if (connection != null && !connection.isClosed()) return;

/* change this to ystring */
connection = DriverManager.getConnection("jdbc:mysql://" + owner.host + ":" + owner.port + "/" + database.get() + "?characterEncoding=latin1&useConfigs=maxPerformance", owner.username, owner.password);
connected = true;
}
} catch (Exception e) {
connected = false;
YExManager.pop(new YexSqlError("YDatabaseWorker couldn't connect to database (" + database.get() + ")"));
}
}

public YQueryResult createQuery(String query) {
final String newQuery = query.startsWith("@") ? query : new YString(query).toString();
try {
return new YQueryResult(connection.prepareStatement(newQuery));
} catch (SQLException e) {
return null;
}
}

}
Loading

0 comments on commit 03dff35

Please sign in to comment.