Re: Is it possible to make a library (dll/so) from separated .d files?

2023-04-03 Thread dog2002 via Digitalmars-d-learn

On Monday, 3 April 2023 at 11:29:12 UTC, Hipreme wrote:

On Monday, 3 April 2023 at 09:08:42 UTC, dog2002 wrote:

Hello. The title sounds weird, but I try to explain it better.

In Unreal Engine and Unity, source code files don't use main() 
and other important functions. They have only a class. Like 
this:


```
class SomeClass: someInterface
{
AFunctionFromTheInterface1()
{

}

AFunctionFromTheInterface2()
{

}
}
```

And then all the source code files will be compiled into a 
single .dll/.so library, so the game engine can use one in a 
game.


I don't know what compiler does Unreal Engine use, but it uses 
C++.



https://github.com/MrcSnm/HipremeEngine/blob/66618c7783d62107bcaad393d5af5b86c9387b34/api/source/hip/api/package.d#L58



Thank you, looks almost what I need!


Re: Is it possible to make a library (dll/so) from separated .d files?

2023-04-03 Thread dog2002 via Digitalmars-d-learn

On Monday, 3 April 2023 at 09:08:42 UTC, dog2002 wrote:

Hello. The title sounds weird, but I try to explain it better.

In Unreal Engine and Unity, source code files don't use main() 
and other important functions. They have only a class. Like 
this:


```
class SomeClass: someInterface
{
AFunctionFromTheInterface1()
{

}

AFunctionFromTheInterface2()
{

}
}
```

And then all the source code files will be compiled into a 
single .dll/.so library, so the game engine can use one in a 
game.


I don't know what compiler does Unreal Engine use, but it uses 
C++.


Is it possible to do so in D?


There are examples of code here 
https://docs.unrealengine.com/5.1/en-US/unreal-engine-for-unity-developers/


Is it possible to make a library (dll/so) from separated .d files?

2023-04-03 Thread dog2002 via Digitalmars-d-learn

Hello. The title sounds weird, but I try to explain it better.

In Unreal Engine and Unity, source code files don't use main() 
and other important functions. They have only a class. Like this:


```
class SomeClass: someInterface
{
AFunctionFromTheInterface1()
{

}

AFunctionFromTheInterface2()
{

}
}
```

And then all the source code files will be compiled into a single 
.dll/.so library, so the game engine can use one in a game.


I don't know what compiler does Unreal Engine use, but it uses 
C++.


Is it possible to do so in D?


Re: How to update terminal output?

2021-03-28 Thread dog2002 via Digitalmars-d-learn

On Sunday, 28 March 2021 at 17:13:02 UTC, Mark Lagodych wrote:

On Sunday, 28 March 2021 at 16:45:29 UTC, dog2002 wrote:
I mean, I want to write a string without a new line. For 
example, some command line applications have progress bars.


For a single line string I use \r. But it doesn't work for a 
multiple line string - the application is just adding new 
lines.


See http://www.climagic.org/mirrors/VT100_Escape_Codes.html
Basically, you just need to do this:

writeln("\033[" ~ cmd);

where cmd is your VT100 command and \033 is an Escape character.

Although some (all?) of that commands do not work in the 
Windows terminal. For instance, you can change background color 
ONLY using Windows API.


Thank you. Seems like \033[A\r does work.


How to update terminal output?

2021-03-28 Thread dog2002 via Digitalmars-d-learn
I mean, I want to write a string without a new line. For example, 
some command line applications have progress bars.


For a single line string I use \r. But it doesn't work for a 
multiple line string - the application is just adding new lines.


How to handle exceptions right?

2021-03-08 Thread dog2002 via Digitalmars-d-learn
I want to call functions according to an exception. In C there is 
the errno function. But if I use try...catch, it shows a trace or 
a message. How do I check an exception type? For example:


std.socket.SocketOSException@std/socket.d(2857): Unable to 
connect socket: Connection refused


??:? [0x459ec5]
??:? [0x4646e6]
??:? [0x44743d]
??:? [0x42c833]
??:? [0x404273]
??:? [0x404cf8]
??:? [0x440a43]
??:? [0x7fdeccecc3f8]
??:? clone [0x7fdeccc98b52]

or

Unable to connect socket: Connection refused

So do I just need to check the exception string? Or there is 
something like errno in C?


Re: What are these functions called and how to implement they?

2021-01-28 Thread dog2002 via Digitalmars-d-learn

On Thursday, 28 January 2021 at 18:56:15 UTC, frame wrote:

On Thursday, 28 January 2021 at 18:27:09 UTC, frame wrote:


[...]


A very simple example:

bool myEventA = true;
bool myEventB = false;

// event source that generates the event (must be called to run)
void source() {
observe(myEventA);
}

// routine that decides what handler to call
void observe(bool event) {
switch (event) {
case true:
onMyEventA(event);
break;

case false:
onMyEventB(event);
break;

default:
assert(0);
}
}

// handler
void onMyEventA(bool event) {
// do something
}

void onMyEventB(bool event) {
// do something
}


Thank you, I'll try it


What are these functions called and how to implement they?

2021-01-28 Thread dog2002 via Digitalmars-d-learn
I saw these functions in some projects. For example: in Dagon 
(https://gecko0307.github.io/dagon/) there are functions like 
onKeyDown. This function doesn't need to call - it checks pressed 
keys every time. Or Update in Unity (game engine). It doesn't 
need to call, but it executes itself every frame.


Okay, maybe it sounds confusing. I just want to understand how to 
implement functions that wait for events and execute code inside 
the function. Something like this:


void onKeyPressed(int key) {
//the code to be executed if a key has been pressed
}




Re: Directory recursive walking

2021-01-15 Thread dog2002 via Digitalmars-d-learn

On Friday, 15 January 2021 at 11:05:56 UTC, Daniel Kozak wrote:
On Fri, Jan 15, 2021 at 10:30 AM dog2002 via 
Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> wrote:



...
Okay, the reason is incredibly stupid: using WinMain instead of
main causes high memory usage. I don't know why, I use the same
code. If I replace WinMain with main, the memory consumption is
about 6 MB.



https://wiki.dlang.org/D_for_Win32


Thank you! Now the application works properly.

And sorry for the dumb questions.


Re: Directory recursive walking

2021-01-15 Thread dog2002 via Digitalmars-d-learn

On Friday, 15 January 2021 at 06:15:06 UTC, dog2002 wrote:

On Thursday, 14 January 2021 at 22:28:19 UTC, Paul Backus wrote:

On Thursday, 14 January 2021 at 20:23:37 UTC, dog2002 wrote:

[...]


What code are you using to copy the bytes? If you're reading 
the whole file into memory at once, that will consume a lot of 
memory.


void func(string inputFile, string outFile, uint chunk_size) {
try {
File _inputFile = File(inputFile, "r");
File _outputFile = File(outFile, "w");

ubyte[] tempBuffer = _inputFile.rawRead(new ubyte[](512));

//doing some operations with the tempBuffer 

_outputFile.rawWrite(tempBuffer);

_inputFile.seek(tempBuffer.length, SEEK_SET);


foreach(_buffer; _inputFile.byChunk(chunk_size)) {
_outputFile.rawWrite(_buffer);
}
_inputFile.close();
_outputFile.close();
}
catch (Throwable) {}

}


Okay, the reason is incredibly stupid: using WinMain instead of 
main causes high memory usage. I don't know why, I use the same 
code. If I replace WinMain with main, the memory consumption is 
about 6 MB.


Re: Directory recursive walking

2021-01-14 Thread dog2002 via Digitalmars-d-learn

On Friday, 15 January 2021 at 06:56:36 UTC, dog2002 wrote:

On Friday, 15 January 2021 at 06:33:55 UTC, Paul Backus wrote:

On Friday, 15 January 2021 at 06:31:18 UTC, Paul Backus wrote:


You can save a little bit of memory here by allocating 
tempBuffer on the stack:


ubyte[512] tempBuffer;
_inputFile.rawRead(tempBuffer[]); // note the explicit []


I made a mistake; this should be:

ubyte[512] tempArray;
ubyte[] tempBuffer = _inputFile.rawRead(tempArray[]);

...with the rest the same as your original version.


Thank you so much! It saves a lot of memory!

And one last question: why the application crashes, if I 
allocate 1 MB array?



ubyte[1024000] tempBuffer;


Solved:

ubyte[] tempBuffer = new ubyte[1024000];


Re: Directory recursive walking

2021-01-14 Thread dog2002 via Digitalmars-d-learn

On Friday, 15 January 2021 at 06:33:55 UTC, Paul Backus wrote:

On Friday, 15 January 2021 at 06:31:18 UTC, Paul Backus wrote:


You can save a little bit of memory here by allocating 
tempBuffer on the stack:


ubyte[512] tempBuffer;
_inputFile.rawRead(tempBuffer[]); // note the explicit []


I made a mistake; this should be:

ubyte[512] tempArray;
ubyte[] tempBuffer = _inputFile.rawRead(tempArray[]);

...with the rest the same as your original version.


Thank you so much! It saves a lot of memory!

And one last question: why the application crashes, if I allocate 
1 MB array?



ubyte[1024000] tempBuffer;


Re: Directory recursive walking

2021-01-14 Thread dog2002 via Digitalmars-d-learn

On Thursday, 14 January 2021 at 22:28:19 UTC, Paul Backus wrote:

On Thursday, 14 January 2021 at 20:23:37 UTC, dog2002 wrote:

About 1000 large files.

I want to replace several first bytes in all the files, so I 
just copy the remaining bytes into a new file. Might this be 
the reason for high memory consumption? If so, is there a way 
not to copy the entire file, just delete first bytes and write 
the replaced bytes into the beginning of the file?


I use Windows x64.


What code are you using to copy the bytes? If you're reading 
the whole file into memory at once, that will consume a lot of 
memory.


void func(string inputFile, string outFile, uint chunk_size) {
try {
File _inputFile = File(inputFile, "r");
File _outputFile = File(outFile, "w");

ubyte[] tempBuffer = _inputFile.rawRead(new ubyte[](512));

//doing some operations with the tempBuffer 

_outputFile.rawWrite(tempBuffer);

_inputFile.seek(tempBuffer.length, SEEK_SET);


foreach(_buffer; _inputFile.byChunk(chunk_size)) {
_outputFile.rawWrite(_buffer);
}
_inputFile.close();
_outputFile.close();
}
catch (Throwable) {}

}


Re: Directory recursive walking

2021-01-14 Thread dog2002 via Digitalmars-d-learn

On Thursday, 14 January 2021 at 16:47:45 UTC, drug wrote:

On 1/14/21 7:06 PM, dog2002 wrote:

On Thursday, 14 January 2021 at 16:01:43 UTC, drug wrote:

On 1/14/21 6:55 PM, drug wrote:


But this method consumes a huge amount of memory (up to 
4 GB and more). Is there a more appropriate way to walk 
directories recursively that does not consume a lot of 
memory?


DirEntry is a struct. First of all I would try this:
```D
foreach(ref entry; dirEntries(path, SpanMode.shallow, false))
```


Does your directory just contain large amount of files?


Yes. I forgot to add it in the original post.


How much files do you have? DirEntry size is 168 bytes only and 
dirEntry is lazy range so I'm curious what is the reason of 
huge memory consumption. Do you use Windows 32 bits between?


About 1000 large files.

I want to replace several first bytes in all the files, so I just 
copy the remaining bytes into a new file. Might this be the 
reason for high memory consumption? If so, is there a way not to 
copy the entire file, just delete first bytes and write the 
replaced bytes into the beginning of the file?


I use Windows x64.


Re: Directory recursive walking

2021-01-14 Thread dog2002 via Digitalmars-d-learn

On Thursday, 14 January 2021 at 16:18:28 UTC, drug wrote:

On 1/14/21 7:06 PM, dog2002 wrote:

On Thursday, 14 January 2021 at 16:01:43 UTC, drug wrote:

[...]


Yes. I forgot to add it in the original post.


Does using `ref` changed anything?
Try following:
```
import std;

void DirIteration(ref DirEntry dir) {
try {
foreach(ref entry; dirEntries(dir, SpanMode.shallow, 
false)) { //SpanMode.shallow allows skip directories if any 
error happens

if (entry.isFile && !entry.isSymlink)
writeln(entry); //Or something instead of this
if (entry.isDir)
DirIteration(entry);
}
}
catch (Throwable) {}
}

void main()
{
auto de = DirEntry(".");
DirIteration(de);
}
```


No, it doesn't. Seems like memory can't clear.


Re: Directory recursive walking

2021-01-14 Thread dog2002 via Digitalmars-d-learn

On Thursday, 14 January 2021 at 16:01:43 UTC, drug wrote:

On 1/14/21 6:55 PM, drug wrote:


But this method consumes a huge amount of memory (up to 4 GB 
and more). Is there a more appropriate way to walk 
directories recursively that does not consume a lot of memory?


DirEntry is a struct. First of all I would try this:
```D
foreach(ref entry; dirEntries(path, SpanMode.shallow, false))
```


Does your directory just contain large amount of files?


Yes. I forgot to add it in the original post.


Directory recursive walking

2021-01-14 Thread dog2002 via Digitalmars-d-learn
I need to make some operations with all the files in a directory 
and subdirectories. Currently, I do it like this:


import std;

void DirIteration(string path) {
try {
foreach(entry; dirEntries(path, SpanMode.shallow, false)) 
{ //SpanMode.shallow allows skip directories if any error happens

if (entry.isFile && !entry.isSymlink)
writeln(entry); //Or something instead of this
if (entry.isDir)
DirIteration(entry);
}
}
catch (Throwable) {}
}

void main()
{
DirIteration("C:\\Users\\angrypuppy\\MyDir");
}

But this method consumes a huge amount of memory (up to 4 GB and 
more). Is there a more appropriate way to walk directories 
recursively that does not consume a lot of memory?


Re: Is there a way to work with processes in Windows without extern(c)?

2021-01-13 Thread dog2002 via Digitalmars-d-learn

On Wednesday, 13 January 2021 at 15:11:40 UTC, Dennis wrote:

On Wednesday, 13 January 2021 at 14:04:52 UTC, dog2002 wrote:

[...]


I don't think this is part of the standard library.
Here's a piece of code I wrote a while ago if that's useful:

[...]


Thank you so much!


Is there a way to work with processes in Windows without extern(c)?

2021-01-13 Thread dog2002 via Digitalmars-d-learn
I need to get a list of processes in Windows (and their pid). I 
could use extern(c) and Process Walking (Process32First, 
Process32Next), but maybe there is a way to get the list by means 
of D?