Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

Commit

Permalink
Finished DLL
Browse files Browse the repository at this point in the history
  • Loading branch information
holly-hacker committed May 4, 2016
1 parent 6977d6f commit 26401ae
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 80 deletions.
30 changes: 10 additions & 20 deletions osu!ibc/FileSelectForm.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Odbc;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace osu_ibc
Expand All @@ -25,13 +18,17 @@ public FileSelectForm()

//snag icon from osu!, purely for esthetic purposes
if (!UpdateFormIcon()) Icon = SystemIcons.Shield;

//bring to front
BringToFront();
Focus();
}

public bool UpdateFormIcon()
{
try {
string path = Process.GetCurrentProcess().MainModule.FileName; //get osu! location
Icon = Icon.ExtractAssociatedIcon(path); //extract icon from file
string path = Process.GetCurrentProcess().MainModule.FileName; //get osu! location
Icon = Icon.ExtractAssociatedIcon(path); //extract icon from file
return true;
}
catch (Exception ex) {
Expand All @@ -46,22 +43,15 @@ private void btnSelect_Click(object sender, EventArgs e)
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Image Files|*.png;*.jpg;*.jpeg|All Files|*"; //allowing all files if users feel like experimenting
if (ofd.ShowDialog() == DialogResult.OK) {
//set string value for path
SelectedFile = ofd.FileName;

//update preview
pbPreview.ImageLocation = SelectedFile;

//enable button
btnDone.Enabled = true;
SelectedFile = ofd.FileName; //set string value for path
pbPreview.ImageLocation = SelectedFile; //update preview
btnDone.Enabled = true; //enable button
}
}

private void btnDone_Click(object sender, EventArgs e)
{
//cleanup?
//set return value to OK
DialogResult = DialogResult.OK;
DialogResult = DialogResult.OK; //set return value to OK
}
}
}
25 changes: 6 additions & 19 deletions osu!ibc/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace osu_ibc
{
static class Helper
{
public static string GetBackgroundFromBeatmap(string beatmapPath) //tested on: v9
public static string GetBackgroundFromBeatmap(string beatmapPath)
{
//[Events]
//0,0,"STBG.jpg"
try {
using (StreamReader sr = new StreamReader(File.OpenRead(beatmapPath))) {
string line;
Expand All @@ -21,7 +18,7 @@ static class Helper
while ((line = sr.ReadLine()) != "[TimingPoints]") {
if (string.IsNullOrWhiteSpace(line)) continue; //[X] bad line
if (line.StartsWith("//")) continue; //[X] comments
if (line.StartsWith("0,")) return line.GetBetween('"'); //[v] proper line
if (line.StartsWith("0,")) return line.GetBetween('"'); //[v] background line
}
}
}
Expand All @@ -32,17 +29,14 @@ static class Helper
return "";
}

public static bool AddBeatmapsToDictionary(string directory, ref Dictionary<string, string> dicks)
public static bool AddBeatmapsToDictionary(string directory, ref Dictionary<string, string> dictionary)
{
bool containsBeatmaps = false;
foreach (string file in Directory.GetFiles(directory).Where(file => Path.GetExtension(file) == ".osu")) {
containsBeatmaps = true;
if (dicks.ContainsKey(file)) continue; //ignore if already read

string img = GetBackgroundFromBeatmap(file); //xxx.jpg, xxx.png
if (!string.IsNullOrEmpty(img)) {
dicks.Add(file, directory+img);
}
if (dictionary.ContainsKey(file)) continue; //ignore if already read
string img = GetBackgroundFromBeatmap(file); //get file name
if (!string.IsNullOrEmpty(img)) dictionary.Add(file, directory + img); //add to dictionary
}
return containsBeatmaps;
}
Expand All @@ -52,12 +46,5 @@ public static string GetBetween(this string str, char bounds)
int firstIndex = str.IndexOf(bounds)+1;
return str.Substring(firstIndex, str.LastIndexOf(bounds) - firstIndex);
}

public static void DebugLog(string message, string title = "DEBUG")
{
#if DEBUG
MessageBox.Show(message, title);
#endif
}
}
}
60 changes: 20 additions & 40 deletions osu!ibc/MainClass.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using EasyHook;

namespace osu_ibc
{
public class MainClass : IEntryPoint
{
//TODO: create hkCreateFileA, in case W doesn't work completely

public static string NewFilePath = ""; //TODO seperate png and jpeg
LocalHook hkCreateFileW;
static Dictionary<string, string> fileDictionary = new Dictionary<string, string>();
public static string NewFilePath = "";
LocalHook _hkCreateFileW; //ascii version not needed
static Dictionary<string, string> _fileDictionary = new Dictionary<string, string>();

//Imports
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr CreateFileW(
[MarshalAs(UnmanagedType.LPWStr)] string filename,
Expand All @@ -40,15 +36,15 @@ public class MainClass : IEntryPoint
[MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes,
IntPtr templateFile);

public MainClass(RemoteHooking.IContext InContext) {}
public MainClass(RemoteHooking.IContext inContext) {}

public void Run(RemoteHooking.IContext InContext) //method that gets called
public void Run(RemoteHooking.IContext inContext) //method that gets called
{
//spawn FileSelectForm
FileSelectForm fsf = new FileSelectForm();
if (fsf.ShowDialog() != DialogResult.OK) {
//user didn't complete file selection, show warning
if (MessageBox.Show("You did not select a file.\nDo you want to use invalid files (yes) or exit (no)?","osu!ibc - Warning!",MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) {
if (MessageBox.Show("You did not select a file.\nDo you want to remove all bg's (yes) or exit (no)?","osu!ibc - Warning!",MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) {
return;
}
}
Expand All @@ -58,51 +54,35 @@ public class MainClass : IEntryPoint

//create hooks for CreateFileA and CreateFileW
try {
hkCreateFileW = LocalHook.Create(LocalHook.GetProcAddress("kernel32.dll", "CreateFileW"), new DCreateFileW(CreateFileWHook), this);
hkCreateFileW.ThreadACL.SetExclusiveACL(new[] {0});
_hkCreateFileW = LocalHook.Create(LocalHook.GetProcAddress("kernel32.dll", "CreateFileW"), new DCreateFileW(CreateFileWHook), this);
_hkCreateFileW.ThreadACL.SetExclusiveACL(new[] {0});
RemoteHooking.WakeUpProcess();
MessageBox.Show("Finished?");
}
catch (Exception ex) {
MessageBox.Show("Exception occured while creating hooks: " + ex.Message);
}

while (true)
{
Thread.Sleep(1000);
}
while (true) Thread.Sleep(1000); //just sleeeep
}

static IntPtr CreateFileWHook(
[MarshalAs(UnmanagedType.LPWStr)] string filename,
[MarshalAs(UnmanagedType.U4)] FileAccess access,
[MarshalAs(UnmanagedType.U4)] FileShare share,
IntPtr securityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
[MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes,
IntPtr templateFile)
static IntPtr CreateFileWHook( [MarshalAs(UnmanagedType.LPWStr)] string filename,
[MarshalAs(UnmanagedType.U4)] FileAccess access,
[MarshalAs(UnmanagedType.U4)] FileShare share,
IntPtr securityAttributes,
[MarshalAs(UnmanagedType.U4)] FileMode creationDisposition,
[MarshalAs(UnmanagedType.U4)] FileAttributes flagsAndAttributes,
IntPtr templateFile)
{
string extension = Path.GetExtension(filename);
switch (extension) {
switch (extension?.ToLower()) {
case ".png":
case ".jpg":
case ".jpeg":

if (!Path.GetDirectoryName(filename).Contains("Songs")) { //TODO get songs directory by reading cfg file, return if not in it
goto end;
}

//if image is read, check if folder contains a .osu file
if (Helper.AddBeatmapsToDictionary(Path.GetDirectoryName(filename), ref fileDictionary)) {
if (extension == Path.GetExtension(NewFilePath)) {
//TODO
}
}

if (!Path.GetDirectoryName(filename).Contains("Songs")) break;
if (Helper.AddBeatmapsToDictionary(Path.GetDirectoryName(filename), ref _fileDictionary)) filename = NewFilePath; //this is a bg
break;
}

end:
return CreateFileW(filename, access, share, securityAttributes, creationDisposition, flagsAndAttributes, templateFile);
}
}
Expand Down
1 change: 0 additions & 1 deletion osu!ibc/osu!ibc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
Expand Down

0 comments on commit 26401ae

Please sign in to comment.