Skip to content

Commit

Permalink
Working menu + massive cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
squ1dd13 committed Feb 2, 2021
1 parent 652bdd3 commit d68063b
Show file tree
Hide file tree
Showing 43 changed files with 819 additions and 1,420 deletions.
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ file(GLOB_RECURSE SOURCE_FILES
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-format-security -fobjc-abi-version=2 -fobjc-arc -o Zinc.dylib")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wno-format-security -fobjc-abi-version=2 -fobjc-arc -o Zinc.dylib")

add_library(zinc SHARED ${SOURCE_FILES} src/RenderWare.h src/shared/RenderWare.cpp src/scripts/ScriptManager.cpp src/scripts/ScriptManager.h src/scripts/Script.cpp src/scripts/Script.h src/shared/UserFolder.cpp src/shared/UserFolder.h src/new/Load.cpp src/new/Hook/NotLogos.h src/new/Menu.mm src/new/Menu.h src/new/Mobile.cpp src/new/Mobile.h)
add_library(zinc SHARED ${SOURCE_FILES} src/scripts/Manager.cpp src/scripts/Manager.h src/scripts/Script.cpp src/scripts/Script.h src/user/Directory.cpp src/user/Directory.h src/Load.cpp src/hook/Func.h src/scripts/Menu.mm src/scripts/Menu.h src/scripts/Mobile.cpp src/scripts/Mobile.h)

message(${CMAKE_CXX_COMPILER})

Expand All @@ -43,6 +43,8 @@ target_link_libraries(zinc
"-framework UIKit"
"-framework Foundation"
"-framework CoreGraphics"
"-framework QuartzCore"
"-framework CoreImage"
substrate
-dylib
)
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Zinc - Zinc Is Not CLEO
<!-- Badges are fun -->
[![forthebadge](https://forthebadge.com/images/badges/made-with-c-plus-plus.svg)](https://forthebadge.com) [![forthebadge](https://forthebadge.com/images/badges/built-with-love.svg)](https://forthebadge.com)

Jailbreak tweak for injecting CLEO scripts into GTA: SA on
iOS. [Video demonstration](https://www.youtube.com/watch?v=6FTkOEV7qnw)
Expand Down Expand Up @@ -26,7 +28,7 @@ work.
### What does it *not* do?

* Provide a script menu ("mod menu") such as that of CLEO Android.
* Load PC scripts (some may work, but don't expect them to).
* Load PC scripts (some may work, but don't expect most to).

## Building

Expand Down
1 change: 1 addition & 0 deletions meta/control
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Package: com.squ1dd13.zinc
Name: Zinc
Depends: mobilesubstrate
Replaces: com.squ1dd13.csios
Version: 0.0.2
Architecture: iphoneos-arm
Description: Load scripts into GTA: San Andreas.
Expand Down
28 changes: 16 additions & 12 deletions src/new/App.mm → src/App.mm
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#include "ObjectiveC.h"
#include "shared/Interface.h"
#include <UIKit/UIKit.h>
#include <cmath>

void processTouches(UIView *view, NSSet *touches, Interface::Touch::Type type) {
#include "user/Touch.h"
#include "hook/ObjectiveC.h"
#include "Logging.h"
#include "scripts/Menu.h"

void ProcessTouches(UIView *view, NSSet *touches, Touch::Type type) {
if ([touches count] == 0) {
return;
}

Interface::Touch::beginUpdates();
Touch::BeginUpdates();
for (UITouch *touch in touches) {
auto oldPos = [touch previousLocationInView:view];
auto pos = [touch locationInView:view];
Expand All @@ -21,7 +24,7 @@ void processTouches(UIView *view, NSSet *touches, Interface::Touch::Type type) {

double time = [touch timestamp];

Interface::Touch(oldX, oldY, x, y, type, time).handle();
Touch(oldX, oldY, x, y, type, time).Handle();
}
}

Expand Down Expand Up @@ -96,11 +99,11 @@ - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[touches anyObject] timestamp]
};

processTouches(self, touches, Interface::Touch::Type::Down);
ProcessTouches(self, touches, Touch::Type::Down);
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
processTouches(self, touches, Interface::Touch::Type::Moved);
ProcessTouches(self, touches, Touch::Type::Moved);
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
Expand All @@ -114,16 +117,17 @@ - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

if (IsMenuSwipe(EAGLViewProperties.touch, endTouch)) {
LogImportant("Activate menu!");

// TODO: Present a script menu.
Scripts::Menu::Show();
} else {
Scripts::Menu::Hide();
}

processTouches(self, touches, Interface::Touch::Type::Up);
ProcessTouches(self, touches, Touch::Type::Up);
EAGLViewProperties.touch.time = -1;
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
processTouches(self, touches, Interface::Touch::Type::Up);
ProcessTouches(self, touches, Touch::Type::Up);
}

- (void)createFramebuffer {
Expand All @@ -134,7 +138,7 @@ - (void)createFramebuffer {
float(self.bounds.size.height * self.layer.contentsScale)
};

Interface::Touch::setViewportSize(size[0], size[1]);
Touch::SetViewportSize(size[0], size[1]);
}

@end
Expand Down
8 changes: 0 additions & 8 deletions src/Core.h

This file was deleted.

27 changes: 27 additions & 0 deletions src/Load.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Created by squ1dd13 on 11/01/2021.
//

#include "hook/Func.h"
#include "user/Touch.h"
#include "scripts/Manager.h"
#include "bridge/Memory.h"
#include "Logging.h"

functionhook GameLoadHook {
void Original(const char *);

// FIXME: Probably runs again when the player loads up another game.
void Body(const char *datPath) {
Original(datPath);

Touch::interceptTouches = true;
Scripts::Manager::Init();
}

HookSave(0x100240178)
}

Constructor {
Log("ASLR slide is 0x%llx (%llu decimal)", Memory::AslrSlide(), Memory::AslrSlide());
}
19 changes: 16 additions & 3 deletions src/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
#include <stdexcept>
#include <string>

enum class MessageType { Normal, Info, Error, Warning, Important };
enum class MessageType {
Normal, Info, Error, Warning, Important
};

[[maybe_unused]] void SendBuf(void *data, size_t length);

#define DEBUG_LOGGING 1

#if DEBUG_LOGGING

template <typename... Args>
[[maybe_unused]] inline void Logf(MessageType messageType, const std::string &format, Args... args) {
int size = snprintf(nullptr, 0, format.c_str(), args...) + 1; // Extra space for '\0'
Expand All @@ -24,8 +30,10 @@ template <typename... Args>
std::unique_ptr<char[]> buf(new char[size + 1]);
snprintf(buf.get() + 1, size, format.c_str(), args...);

static std::ofstream stream = std::ofstream("/var/mobile/Documents/Zinc.log",
std::ofstream::out | std::ofstream::trunc);
static std::ofstream stream = std::ofstream(
"/var/mobile/Documents/Zinc.log",
std::ofstream::out | std::ofstream::trunc
);

if (stream) {
stream << (char *)(buf.get() + 1) << '\n';
Expand All @@ -38,6 +46,11 @@ template <typename... Args>
SendBuf(buf.get(), size);
}

#else
template <typename... Args>
[[maybe_unused]] inline void Logf(MessageType messageType, const std::string &format, Args... args) {}
#endif

#define Log(f, ...) Logf(MessageType::Normal, f, ##__VA_ARGS__)
#define LogError(f, ...) Logf(MessageType::Error, f, ##__VA_ARGS__)
#define LogInfo(f, ...) Logf(MessageType::Info, f, ##__VA_ARGS__)
Expand Down
74 changes: 0 additions & 74 deletions src/RenderWare.h

This file was deleted.

7 changes: 4 additions & 3 deletions src/shared/Addresses.h → src/bridge/Addresses.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
// Created on 21/10/2020.
//

#ifndef Zinc_CMAKE_ADDRESSES_H
#define Zinc_CMAKE_ADDRESSES_H
#pragma once

#include "Memory.h"

Expand All @@ -28,6 +27,8 @@ namespace Memory::Addresses {
NameAddress(0x1001d0f40, advanceGameScripts);

NameAddress(0x1004e831c, handleTouch);

// 1.0f for normal speed, less for slower.
NameAddress(0x1007d3b18, timeScale);
}

#endif // Zinc_CMAKE_ADDRESSES_H
28 changes: 28 additions & 0 deletions src/bridge/Memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#pragma once

#include "Types.h"
#include <mach-o/dyld.h>

namespace Memory {
inline uint64 AslrSlide() {
static auto slide = _dyld_get_image_vmaddr_slide(0);
return (uint64)slide;
}

// Offset pointer by ASLR slide (and also cast the result).
template <typename OutType, typename InType>
inline OutType Slid(InType inValue) {
return OutType(uint64(inValue) + AslrSlide());
}

// Offset pointer by ASLR slide, cast it and dereference it.
template <typename OutType, typename InType>
inline OutType Fetch(InType addr) {
return *(OutType *)(Slid<void *>(addr));
}

template <typename Return = void, typename... Args>
inline Return Call(uint64 address, Args... args) {
return Memory::Slid<Return (*)(Args...)>(address)(args...);
}
}
2 changes: 0 additions & 2 deletions src/Types.h → src/bridge/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#define DeclareFunctionType(name, ret, ...) typedef ret (*name)(__VA_ARGS__)

#define squished __attribute__((packed))

#include <cstdint>
#include <string>

Expand Down
File renamed without changes.
File renamed without changes.
15 changes: 7 additions & 8 deletions src/new/Hook/ObjectiveC.mm → src/hook/ObjectiveC.mm
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Objective-C hooking stuff. This replaces the Logos %hook and %orig.
// Lots of this code appeared in some form in https://github.com/Squ1dd13/MaxOS.

#include "new/ObjectiveC.h"
#include "ObjectiveC.h"
#include <Foundation/Foundation.h>
#include <iostream>
#include <set>
#import <Logging.h>

// Finds the original implementation of the method and calls it.
void *callOrig(SEL hookedSelector, id target, ...) {
Expand Down Expand Up @@ -81,16 +82,15 @@ bool hookMethodName(SEL name, Class target, Class hook) {
Method hookMethod = class_getInstanceMethod(hook, name);

if (not originalMethod or not hookMethod) {
std::cout << "either original or hooked method not found\n";
LogError("either original or hooked method not found");
return false;
}

const char *origType = method_getTypeEncoding(originalMethod);
const char *hookType = method_getTypeEncoding(hookMethod);

if (std::strcmp(origType, hookType) != 0) {
std::cout << "type encoding mismatch - method " << sel_getName(name) << " should return " << origType
<< ", but hook returns " << hookType << '\n';
LogError("type encoding mismatch - method %s should return %s, but hook returns %s", sel_getName(name), origType, hookType);
return false;
}

Expand All @@ -100,8 +100,7 @@ bool hookMethodName(SEL name, Class target, Class hook) {
SEL origSelector = NSSelectorFromString([@"original_imp_" stringByAppendingString:NSStringFromSelector(name)]);

if (not class_addMethod(target, origSelector, targetImplementation, origType)) {
std::cout << "failed to add orig method for selector " << sel_getName(name) << " to class "
<< class_getName(target) << '\n';
LogError("failed to add orig method for selector %s to class %s", sel_getName(name), class_getName(target));
return false;
}

Expand Down Expand Up @@ -133,14 +132,14 @@ bool HookClass(const char *hookName, const char *targetName, bool meta) {
Class targetClass = meta ? objc_getMetaClass(targetName) : objc_getClass(targetName);

if (not targetClass) {
std::cout << "could not find target class " << targetName << '\n';
LogError("could not find target class %s", targetName);
return false;
}

Class hookClass = meta ? objc_getMetaClass(hookName) : objc_getClass(hookName);

if (not hookClass) {
std::cout << "could not find hook class " << hookName << '\n';
LogError("could not find hook class %s", hookName);
return false;
}

Expand Down
Loading

0 comments on commit d68063b

Please sign in to comment.